Communication Between Froms...

I currently have a form that I am using to store variables/arrays/etc. I have just created a second form. The form is launched when the user selects a button. I have launched the form by simply instantiating the object, like this:

Form2 f = new Form2();

f.Show();

However, there is data that I need to retrieve and display from the first form (Form1). Would anyone know how I could go about doing this

Thanks,



Answer this question

Communication Between Froms...

  • mike_cbc

    What you've been recommended is a hack. Instead what you need to do is create properties in Form2, which you set with values from Form1 between creating Form2 and calling Show.

    Actually, because you call Show and not ShowDialog, f should be a member variable.

    I hope this is not your actual code, your classes and variables should have more meaningful names than Form1, Form2 and f.

    If you need to communicate between forms, use delegates, they are basically events that you define and call, so form2 can call a function in form1, or vice versa.



  • scottwnelson

    Form 1

    --------------------------------------------------------------------------

    public class Form1 : System.Windows.Forms.Form

    {

    int num = 0;

    }

    private void menuItem8_Click(object sender, System.EventArgs e)

    {

    Form2 f = new Form2();

    f.Show();

    }

    Form 2

    --------------------------------------------------------------------------

    Form1 g = new Form1();

    g.num = 2;


  • ChrisM01

    That's precisely how you do it. myVariable would be a property in your form and the forms load event would take variables that were passed in, and populate UI elements with them.



  • _Francesco

    which version are you working with 2.0


  • zhangmike

    I'm receiving the following error with that code:

    'System.Windows.Forms.Application' does not contain a definition for 'OpenForms'


  • dark_bringer

    Thanks for your help cgraus,

    However, I receive the following error on compile:

    'Phone.Form1.num' is inaccessible due to its protection level

    I haven't specified an access type (public,private, etc).  Shouldn't I have full access to the variable due to it being part of the same class


  • GuesWho

    Is there anyway to do this by instantiating objects.

    For example:

    Form1 test = new Form1();

    test.myVariable = 5;


  • LisandroRocha

    I have searched google for a few examples, but I am having a difficult time understanding the concept.


    As a side note, once I retrieve the variable in Form2, I do not need to modify the value, I just to display it. Is there anyway I am able to do this while having the variable set as 'private' Or will it be best it understand the concept of delegates


  • glocklt4

    Use the following syntax

    Form1 f=(Form1)Application.OpenForms["Form1"];





  • nastynate

    Well, you need to post some code so I can see what's going on. But, you have full access to variables in Form1, if your code is within Form1, yes.

  • rrossenbg

    Form1 g = new Form1();

    g.num = 2;

    This is a waste of time.

    1 - g.num is being called from Form2, and as it is private, you cannot access it

    2 - even if you did, it would be on a different instance of Form1 to the one that is being used by your progra,

    if you want to send messages from Form2 back to Form1, like I said, you need to define a delegate, which would be defined in Form2, hooked to a function in Form1 when you create Form2 ( which should STILL be a member variable ), and called from Form2, taking num as an argument, for example, so it gets passed to a function in Form1. I recommend doing a google on delegates VB.NET and you'll find lots of examples.



  • Aristocrates

    .NET 2003
  • marcotoon

    OK, in that case, just do what I said in the first place

    in Form2

    private int _n;

    public int N

    {

    set { _n = value; }

    get { return _n;}

    }

    private void Form2_OnLoad(object sender, EventArgs ea) // This needs to be set up as an event, not just typed int

    {

    TheTextBoxThatDisplaysN.Text = _n.ToString();

    }

    in Form1:

    Form2 theBadlyNamedForm; // Note that I made it a member variable

    ....

    theBadlyNamedForm.N = this.N;

    theBadlyNamedForm.Show();



  • vnavna

    cgraus, thanks for your help.  It turns out that I did need to munipulate the variable.  I found a great tutorial on delegates and my form passes the variable perfectly!


  • Communication Between Froms...