I have a problem with some code, the first bit of code works fine the data is populated to the open form "testform" textbox1 from the contents of textbox1 on another form say customers form.
testform form1 = (testform)Application.OpenForms["testform"];
form1.Controls["textBox1"].Text = this.textBox1.Text;
Yet this code below which populates the same data to another form already open "bulkitemloadrequestform" throws the error "Object reference not set to an instance of the object"
bulkitemloadrequestform form2 = (bulkitemloadrequestform)Application.OpenForms["bulkitemloadrequestform"];
form2.Controls["textBox5"].Text = this.textBox1.Text;
I can't seem to workout why this problem is occuring?
Help much appreciated.
Loading
theLizardPosted Mar 23, 2010, 10:25 PM
Just one question, seems a long winded way to get to the text box, form2.Controls["tabControl1"].Controls["tabPage1"].Controls["panel2"].Controls["textBox5"].Text = this.textBox1.Text; is there a specific reason for doing it this way?
Barry MilburnPosted Mar 23, 2010, 10:13 PM
Solved the problem. It was because I was trying to pass the data from form2 directly to textBox5 on form1. However I forgot that the object textBox5 was not sitting on just on a form alone.
The object textBox5 was on a panel to which was on a tabpage to which was on a tabcontrol. Hence why got Null error trying to reference the textBox5 directly. i.e. form2.Controls["textBox5"].Text = this.textBox1.Text; duh! :-)
as this code works fine:
form2.Controls["tabControl1"].Controls["tabPage1"].Controls["panel2"].Controls["textBox5"].Text = this.textBox1.Text;
Next time i'll use a dialogue form.
theLizardPosted Mar 23, 2010, 9:36 PM
what are you trying to do?
I mean, why are you doing things this way, is there a specific reason.
form1.Controls["textBox1"].Text = this.textBox1.Text;
this form1.Controls["textBox1"].Text I understand but who owns this.TextBox1?
If form1 owns textbox1 then why are you doing this form1.Controls["textBox1"].Text = this.textBox1.Text;
bulkitemloadrequestform form2 = (bulkitemloadrequestform)Application.OpenForms["bulkitemloadrequestform"];
the above implies the form is open, is this correct?
Which object is throwing the error, is it the form2 or the form2.Controls["textBox5" or the this.textBox1.Text;
Just trying to get my head around the problem.