0

I have the following: I have a GUI with two forms. Form2 is opened via Form1 by a button. Both forms have Textboxes and I want them to communicate with each other (one form can grab the entries of textboxes from another form). What I did now in Form2 is:

    private Form1 m_form = null;

    public Form2(Form1 f)
    {
        InitializeComponent();

        m_form = f;
    }

and for the Textboxes functions like:

    public String getLocation()
    {
        return LocationBox.Text;
    }

That works fine. So Form2 can read the entries from Form1. Now I wanted Form1 to read the textbox entries from Form2 and tried the same thing (which is probably wrong):

    private Form2 m_form2 = null;

    public Form1(Form2 f2) 
    {
        InitializeComponent(); 
        m_form2 = f2;
    }

and then some functions like the one I've posted but everytime I want to read a textbox with Form1 which is in Form2 I get "null" and "NullReference" Exception. Where is the error?

EDIT: Ok I solved a part. Adding

        Form2 m_form2 = new Form2(this);
        m_form2.Show();

solves the problem with the NullReferenceException. Without the line m_form2.Show()

it passes empty strings but now everytime I hit a button the form2 appears.

uzi42tmp
  • 271
  • 2
  • 9
  • 22

3 Answers3

2

I think the problem is that you never call the consturctor public Form1(Form2 f2) . When you open Form2 from the first one you have to save that instance within your instance of Form1:

void createForm2() {
    Form2 frm2 = new Form2(this);
    this.m_form2 = frm2;
}
MakePeaceGreatAgain
  • 35,491
  • 6
  • 60
  • 111
1

try this in Form1:

this.TextBoxName.Text = m_Form2.getLocation();

or in Form2:

m_form.TextBoxName.Text = this.getLocation();

Note in Form1 when call form 2:

m_Form2 = new Form2(this);
//do anything with your code
MasterLuV
  • 396
  • 1
  • 17
  • Does not work. He has problems with "this", and can't even find the Textbox. – uzi42tmp Jan 09 '15 at 07:03
  • you should modify TextBox is Public – MasterLuV Jan 09 '15 at 07:04
  • when you code in Form1 - this mean Form1, when you code in Form2 - this mean Form2 – MasterLuV Jan 09 '15 at 07:06
  • He sais that m_form is a field but gets used like a type and this.getLocation() has no return value. When I put the m_form2 = new Form2(this) in the null exception disappears but he now passes empty strings. Coding in Form2 does work but not in coding in Form1 – uzi42tmp Jan 09 '15 at 07:23
  • http://stackoverflow.com/questions/1665533/communicate-between-two-windows-forms-in-c-sharp see this – MasterLuV Jan 09 '15 at 07:46
  • Ok now that's weird. Thanks for the link. I added the line "m_form2.Show();" under "Form2 m_form2 = new Form2(this);" and it works but everytime I hit a button the second form opens. I don't undestand this! – uzi42tmp Jan 09 '15 at 08:06
  • add this if(m_form2 == null){m_form2 = new Form2(this);}. every you hit button you create 1 new instance. Add if in front of can avoid that – MasterLuV Jan 09 '15 at 08:22
  • Does not work. I can't make a new instance of Form2 in an if statement – uzi42tmp Jan 09 '15 at 09:38
  • i don't really know what you want @@. use if(m_form2 == null){m_form2 = new Form2(this);} m_form2.Show(); if you want only create 1 instance. use only m_form2 = new Form2(this); m_form2.Show(); if you want create more and more instance after hit button – MasterLuV Jan 09 '15 at 12:11
1

For sending values between two forms, you may

1-> Send the values in the constructor of the second form. You may create a paramterized constructor and send the values when you initialize the form as :

Form1 obj = new Form1(Object);

2-> You may take a reference in to your first form in the second form.

In second form,

public Form1 objForm1;
and in First Form,

Form2 objForm2=new Form2();
Form2.objForm1=this;

and then you can use Form2's objForm1 to refer to Form1's textbox or any control.

Consider you want to send all values from Form1 to Form2

In your second form you must have a variable of type Form1 that refers to the prev form. So in second form,

public Form1 objForm1;
and then you need to send the current instance of the Form1 to Form2 as

Form2 objForm2=new Form2();
Form2.objForm1=this;

i.e. the objForm1 that you created in Form2 refers to this instance of Form1.

Now in Form2 you may use any of Form1's control or variable as,

Form1.TextBox1 or Form1.Variable

Tanuj Wadhwa
  • 2,025
  • 9
  • 35
  • 57