Passing info from one from to another

  • Thread starter Thread starter thawk
  • Start date Start date
T

thawk

I have tried to pass information from one form to another
using the described method in the documentation, but
everytime I attempt to do this I get an error message that
states that I have a null reference. I am entering data
into a text box on one form and attempting to use that
data on a second form. The problem is that the data never
gets to the second form. Any ideas?
 
Probably need more information about how the two forms are instantiated.
Are you passing/assigning a reference to form1 in form2?
If form1 has a text box, you could create a property to return
the value of the text box but form2 would need a reference to form1.
You could accomplish this when they are created:

MyFormClass form1 = new MyFormClass();
MyFormClass form2 = new MyFormClass();
form2.MyFormClassPointer = form1; //MyFormClassPointer is a public variable or property defined in the MyFormClass...


....or something like that....

Steve
 
Pass the form1 as a paramter in the constructor of Form2

Form1 myform1 = new Form1();

//pass form 1 as parameter in the constructor
Form2 myform2 = new Form2(myform1 );
 
If Form1 creates Form2 then you can have a Form1 field in Form2:

public Form1 form1; // or create a property instead of a public field

Then when Form1 creates Form2, assign the public field a value. This code
is in Form1:

Form2 form2 = new Form2();
form2.form1 = this;

Now, the Form2 instance can pass values back to the Form1 instance.

Dale
 
I took the information I used to pass between forms directly from the msdn library and it didn't appear to work.

private Form1 otherForm;
private void GetOtherTextBox()
{
textBox1.Texxt = otherForm.TextBox1.Text;
}

on form2

private TextBox TextBox1
{
get
{
textBox1.Text = TextBox1.Text;
}
}

something like this, I don't have the page in front of me, it is at work and I am now at home. This is where i get the null reference and can't figure out why...
 
Back
Top