Hello all.....
I'm having a hard time understanding how to read/write to the contents of controls on diffient forms.
If I create a Windows Form project with two forms..... add a text box to each...now I add the following code to form1.....
Public Class Form1
Inherits System.Windows.Forms.Form
Dim f2 As New Form2
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
f2.Show()
End Sub
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
f2.TextBox1.Text = TextBox1.Text
End Sub
End Class
So when you run the app, what ever you type in form1, textbox1's control will appear in form2, textbox1 control....
What I can't do is make it so that what's typed in form2, textbox1 appears in form1, textbox1....
Thanks in advance, hope that makes sense.
I have read the following article on MSDN: (Note to MS: Can we have MSDN article numbers )
http://msdn.microsoft.com/library/default.asp url=/library/en-us/dv_vstechart/html/vbtchWorkingWithMultipleFormsInVisualBasicNETUpgradingToNET.asp
Wayne Taylor (kryptos)
If you wish to e-mail me, then please remove 'online' from my e-mail addy.

forms
Mary Chipman
public class Form2 : Form
{
private Form1 frm1;
// Keep the default constructor
public Form2(){...}
// And add this one
public Form2(Form1 form1) : this()
{
frm1 = form1;
}
}
Then, when you want to update Form1's info, you have a reference to it:
frm1.TextBox1.Text = "";
I just want to add one thing, however. In the line above, you can access TextBox1 because it's declared as public. I showed it that way becuase that's how you have it declared in your class, according to your example.
This is generally regarded as a bad practice. A much better way would be to keep the control private or protected (as they are by default) and expose a public property that represents the text you're interested in.
public string MyTextField
{
get{ return this.TextBox1.Text; }
set{ this.TextBox1.Text = value; }
}
This way, when you decide to change the implementation on one of the forms, the other won't have to be changed as well. For example, if you were to decide tomorrow that Form1 should have a label display the text instead of a text box, you'd have to alter the code in both forms. Just change the setter to this.Label1.Text = value; It also aloows you to add validation logic to make sure that the caller is passing a valid value. As it stands now, you'd have to change both forms to accomodate the change and still have no control over what gets shown.
Besides making it easier for you to make changes as your app grows, it also gets you away from passing control references from form to form, which is not a very good idea in itself.