Answered by:
How different between these 2 methods of pass variable between form ?
Question
-
1. Created class then share varriable
2. Just sent it directly to target form
Form2.text1.text = "TEST1"
How different between these 2
Saturday, November 16, 2013 7:10 AM
Answers
-
<copied>
And what shohld be the best way to pass variable between form (reliable and fast)
<end>
Just pass the class/object as a parm on the form's constructor.
http://www.dreamincode.net/forums/topic/90845-passing-parameters-to-forms-while-creating-them/
- Proposed as answer by Frank L. Smith Sunday, November 17, 2013 12:41 AM
- Marked as answer by Hemkoe Monday, November 18, 2013 3:07 AM
Saturday, November 16, 2013 1:06 PM -
Hi,
darnold924 has given you a link that shows how you can pass a variable to the 2nd form when it is opened. Below is a small example of doing this. You can test it by creating a new Form project and then adding a new form "Form2" to it. Then drag a Button onto Form1 and a TextBox on Form2.
Form1 Code
Public Class Form1 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim txt As String = "Show this string in Form2 TextBox" Dim frm2 As New Form2(txt) frm2.Show() End Sub End ClassForm2 Code
Public Class Form2 Public Sub New(ByVal str As String) MyBase.New() InitializeComponent() TextBox1.Text = str End Sub End ClassHowever, if you also want to be able to retrieve the text in the TextBox on Form2 from Form1 then you could add a Public Property to Form2 and retrieve the text from Form2`s TextBox when Form2 is closing. Here is an example of doing this. You can use the same setup of the two forms as described above.
Form1 Code
Public Class Form1 Dim Frm2 As Form2 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Frm2 = New Form2 AddHandler Frm2.FormClosing, AddressOf Frm2_Closing 'Add a handler so Form1 knows when Form2 is closing Frm2.TxtBoxText = "Show this string in Form2 TextBox" Frm2.ShowDialog() End Sub Private Sub Frm2_Closing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Me.Text = Frm2.TxtBoxText 'Set Form1`s title text to the text in Form2`s TextBox when Form2 is closing End Sub End Class
Form2 Code
Public Class Form2 Public Property TxtBoxText() As String Get Return TextBox1.Text End Get Set(ByVal value As String) TextBox1.Text = value End Set End Property End Class
- Edited by IronRazerz Saturday, November 16, 2013 2:52 PM
- Proposed as answer by Frank L. Smith Sunday, November 17, 2013 12:41 AM
- Marked as answer by Hemkoe Monday, November 18, 2013 3:07 AM
Saturday, November 16, 2013 2:49 PM
All replies
-
And what shohld be the best way to pass variable between form (reliable and fast)
Thx
Saturday, November 16, 2013 7:12 AM -
<copied>
And what shohld be the best way to pass variable between form (reliable and fast)
<end>
Just pass the class/object as a parm on the form's constructor.
http://www.dreamincode.net/forums/topic/90845-passing-parameters-to-forms-while-creating-them/
- Proposed as answer by Frank L. Smith Sunday, November 17, 2013 12:41 AM
- Marked as answer by Hemkoe Monday, November 18, 2013 3:07 AM
Saturday, November 16, 2013 1:06 PM -
Hi,
darnold924 has given you a link that shows how you can pass a variable to the 2nd form when it is opened. Below is a small example of doing this. You can test it by creating a new Form project and then adding a new form "Form2" to it. Then drag a Button onto Form1 and a TextBox on Form2.
Form1 Code
Public Class Form1 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim txt As String = "Show this string in Form2 TextBox" Dim frm2 As New Form2(txt) frm2.Show() End Sub End ClassForm2 Code
Public Class Form2 Public Sub New(ByVal str As String) MyBase.New() InitializeComponent() TextBox1.Text = str End Sub End ClassHowever, if you also want to be able to retrieve the text in the TextBox on Form2 from Form1 then you could add a Public Property to Form2 and retrieve the text from Form2`s TextBox when Form2 is closing. Here is an example of doing this. You can use the same setup of the two forms as described above.
Form1 Code
Public Class Form1 Dim Frm2 As Form2 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Frm2 = New Form2 AddHandler Frm2.FormClosing, AddressOf Frm2_Closing 'Add a handler so Form1 knows when Form2 is closing Frm2.TxtBoxText = "Show this string in Form2 TextBox" Frm2.ShowDialog() End Sub Private Sub Frm2_Closing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Me.Text = Frm2.TxtBoxText 'Set Form1`s title text to the text in Form2`s TextBox when Form2 is closing End Sub End Class
Form2 Code
Public Class Form2 Public Property TxtBoxText() As String Get Return TextBox1.Text End Get Set(ByVal value As String) TextBox1.Text = value End Set End Property End Class
- Edited by IronRazerz Saturday, November 16, 2013 2:52 PM
- Proposed as answer by Frank L. Smith Sunday, November 17, 2013 12:41 AM
- Marked as answer by Hemkoe Monday, November 18, 2013 3:07 AM
Saturday, November 16, 2013 2:49 PM