Accessing a forms textbox control from a second form and Vis versa

G

Guest

I am reading the book Teach Yourself Microsoft Visual Basic .Net 2003 in 21
Days.

I am having trouble getting one of the exercises to work at the end of day 4.

Exercises:

1. Create a new multiple-form application that displays two forms when it
starts, each
with a text box. Add code to each text box’s TextChanged event (this is the
default
event for text boxes, which occurs when the text in the text box changes;
just double-
click a text box to bring this event handler up in a code designer) so that
when
you change the text in one text box, the new text also appears automatically
in the
text box in the other form, keeping the two text boxes in the two different
forms
synchronized.


This is what I have:
I have my main form "Form1" with a textbox which is loaded at the
application start. I have a second form "Form2" with a textbox that I load
in "Form1's" onload event handler by creating a new instance of "Form2". Now
I have no problem getting what is typed in "Form1's" textbox to show up in
the new instance of "Form2". I have having a problem getting what is typed
in "From2's" textbox to show up in "Form1's" textbox. This is just supposed
to be basic code to use to do this, but I am not sure howto get access to
"From1" from inside the instance of "Form2" that I created.


Public Class Form1
Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "
-Deleted to shorten post
#End Region
Dim frmTwo As New Form2


Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
frmTwo.Show()
End Sub

Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles TextBox1.TextChanged
frmTwo.TextBox1.Text = Me.TextBox1.Text
End Sub
End Class


Public Class Form2
Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "
-Deleted to shorted post
#End Region

Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles TextBox1.TextChanged
'I am not sure how to get access to Form1.textbox1.text from inside
here
End Sub
End Class


Thanks
 
P

Phil G.

Hi ????,

You need to declare your dynamic textbox using the WithEvents keyword. You
will then see that you can keep all of your code in Form1's class:-

(Form1)

Dim frm2 as New Form2
Dim WithEvents txt2 as New Textbox

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
With txt2
.Name = "tbox2"
.Size = New Size(New Point(120, 24))
.Location = New Point(60, 40)
End With
frm2.Controls.Add(txt2)
frm2.Show()
End Sub

Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles TextBox1.TextChanged
txt2.Text = TextBox1.Text
End Sub

Private Sub txt2_TextChanged(ByVal sender As Object, ByVal e As
System.EventArgs) Handles txt2.TextChanged
TextBox1.Text = txt2.Text
End Sub

There are of course other ways! HTH, Phil
 
G

Guest

OK, I will try this out. But one thing I would like to mention is in the
Visual Studio .Net GUI I have two forms. Form1 which is my startup form, and
Form2 which I added to the solution explorer and placed a Textbox on. From
within my startup form From1's load event handler I am creating a new
instance of Form2 which has the textbox placed on it. I see in your code you
are dynamically adding the textbox within Form1 to Form2 which will
essentially give both textboxes access to each other.
 
P

Phil G.

OK. I still prefer my original suggestion but if for whatever reason you
need/prefer to create the textbox at design time then this example will
work..............I am not saying it is pretty or the only way but hey, it
works!!

(Form1)

Dim frm2 As New Form2

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
frm2.Show()
frm2.frm1 = Me
End Sub

Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles TextBox1.TextChanged
frm2.TextBox1.Text = TextBox1.Text
End Sub

(Form2)

Public frm1 As New Form

Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles TextBox1.TextChanged
Dim ctrl As TextBox
For Each ctrl In frm1.Controls
If TypeOf ctrl Is TextBox Then
ctrl.Text = TextBox1.Text
End If
Next
End Sub

Rgds, Phil
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top