I have created two forms say Form1 and Form2. Form1 consists of a label and a button which shows Form2. Now Form2 contains a text box and a button. When the button in Form2 is clicked, the content in textbox should be shown to the label in Form1. This can be repeated as long as user want. Until now I can pass data to unopened Form. But I can not update the data, when both forms are active.
In Form1, I have declared a global string
Public Property message As String
In Form2 the button click event is as follow
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim obj As New Form1
obj.message = TextBox1.Text
End Sub
Even I have tried
obj.label1.Text = TextBox1.Text
But these were not successful. Please help to achieve the target. Please remeber I want to achieve this using two normal Forms not any special type of Forms like MDI.
You can pass a reference to Form1 into Form2 via the Show()
method. This reference can be retrieved in Form2 using the .Owner
property.
In Form1:
Dim f2 As New Form2()
f2.Show(Me) ' <-- passing reference to Form1 into Form2
In Form2:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim f1 As Form1 = DirectCast(Me.Owner, Form1)
f1.message = TextBox1.Text
End Sub