'Passing' a variable from a child form to the parent... How???

csharp Japan
  • 13 years ago
    So...  That's the gist of it.  I have my main form, Form1.  And it has a child form, Form2.

    Now, it doesn't matter in the slightest if Form2 is shown or not.  Or if it's even active.  I want it, at the appropriate time (there's a few instances where this would happen), to retrive some data from Form1, which are variables.

    I also want Form2, on the press of a button (easy part) to update the variables on Form1(THIS is the hard part), and then display those variables in the appropriate area (the other easy part)...


    It *SHOULD* be easy enough, I know.  But for whatever reason or another, I just can NOT find how to do this.  It's just ever so slightly beyond me...

    So any help would be nice, THANKS!!!










  • 13 years ago

    In your main form, pass an object via the child form's constructor (chartDataHandler in this example):

       private void btnPick_Click(object sender, EventArgs e)
            {      
                FormCostPicker formCostPicker = new FormCostPicker(chartDataHandler);
                formCostPicker.ShowInTaskbar = false;


                DialogResult r = formCostPicker.ShowDialog();

                if (r == DialogResult.OK)
                {
                    tbCost.Text = chartDataHandler.selectedItem.ToString();              
                }
            }



     

    In your second form, pick up and use the object you passed in:

       public partial class FormCostPicker : Form
        {
            public ChartDataHandler chartDataHandler;

            public FormCostPicker(ChartDataHandler chartDataHandler)
            {
                InitializeComponent();
                this.chartDataHandler = chartDataHandler;
              
            }





            private void btnOK_Click(object sender, EventArgs e)
            {
                chartDataHandler.selectedItem = Item;


                DialogResult = DialogResult.OK;
                Close();
            }

    This could be tided up a little, but that should point you in the right direction.

    HTH

  • 13 years ago
    I'm sorry, but it don't work.
  • 13 years ago
    You can solve it using properties in child form.

    Form 02



    private Object _YourObject;

    public object YourObject
    {
        get {return _yourObject;}
        set {_YourObject = value;}
    }


    Form 01



    Form2 form2 = new Form2();
    form2.YourObject = new form2.YourObject();
    form2.Show();

    After closing form2, you can use form2.YourObject.

    Regards,
    Ibrahim Faisal



















  • 13 years ago
    OK, could you post the code that does not work for you here please?  Thanks!

Post a reply

Enter your message below

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Some people, when confronted with a problem, think "I know, I’ll use regular expressions." Now they have two problems.” - Jamie Zawinski