Question Passing variable between 2 forms

andimanro

Member
Joined
Jun 7, 2010
Messages
18
Programming Experience
1-3
i have 2 forms -> form 1 and form 2
in form 1 -> 1 button
in form 2 -> 1 textbox and 1 button

when I click button in form 1, it will show the form 2..

how can i pass the variable (data from textbox in form 2) into a variable in form 1 after clicking the button in form 2 ?

can anyone give me a sample code to do that ?

Thnx
 
dear jmcilhinney...

which property should i use in form 2 ? can you give me a sample code to do that ? by the way, i'm still newbie in vb.net... thnx
 
The property does exist until you declare it. If you don't know how to declare a property then you can look it up, or you could just use a function instead. A property would be more appropriate but the end result will be the same. Any beginner tutorial will show you how to declare properties, e.g. lesson 12 here:

Microsoft Visual Basic .NET tutorials for Beginners
 
VB.NET:
Public Class Form2
    Dim m_strReturnVariable As String = ""

    Public Property ReturnVariable() As String
        Get
            Return m_strReturnVariable .Trim
        End Get
        Set(ByVal value As String)
            m_strReturnVariable = value.Trim
        End Set
    End Property

    Private Sub Form2_FormClosing(...) Handles Me.FormClosing
         ReturnVariable = txtUserEntry.Text
    End Sub

VB.NET:
'In Form1

Dim frm As New Form2
frm.ShowDialog
Me.lblResult.Text = frm.ReturnVariable
frm.Dispose
 
VB.NET:
Public Class Form2
    Dim m_strReturnVariable As String = ""

    Public Property ReturnVariable() As String
        Get
            Return m_strReturnVariable .Trim
        End Get
        Set(ByVal value As String)
            m_strReturnVariable = value.Trim
        End Set
    End Property

    Private Sub Form2_FormClosing(...) Handles Me.FormClosing
         ReturnVariable = txtUserEntry.Text
    End Sub

VB.NET:
'In Form1

Dim frm As New Form2
frm.ShowDialog
Me.lblResult.Text = frm.ReturnVariable
frm.Dispose

Why bother with the backing field and setting it in the FormClosing event handler? Why not simply return the TextBox's Text property directly from the property?
 
Why bother with the backing field and setting it in the FormClosing event handler? Why not simply return the TextBox's Text property directly from the property?

Agreed, many ways of slaying the same beast. In fact you dont even need of to create your own property in the second form, you can return directly from the textbox. I thought you were explaing creating custom properties and offered an example of such.The formclosing event I used just for this example figuring it would be easier for him to understand.

VB.NET:
'Code in Form1 to show Form2
'No code needed in Form2

Private Sub btnShowChildForm_Click(...) Handles btnShowChildForm.Click

        Dim frm As New Form2

        frm.ShowDialog()
        MsgBox(frm.txtUserInput.Text)
        frm.Dispose()

End Sub
 
The default Friend access modifier for controls generated backing field exist to make these things easier accessible within the project, it is RAD-friendly. For exclusive usage within project I would use the form field for the control as in Tom's last example, and not bother setting up also a property (unless that provided better access/readability).

If the class was designed to be accessed from another project the control backing field should probably not be made available, instead the parent class should expose relevant properties, or a readonly property to the control if the whole control object should be accessible to be read/modified but not dereferenced.
 
Agreed, many ways of slaying the same beast. In fact you dont even need of to create your own property in the second form, you can return directly from the textbox. I thought you were explaing creating custom properties and offered an example of such.The formclosing event I used just for this example figuring it would be easier for him to understand.

VB.NET:
'Code in Form1 to show Form2
'No code needed in Form2

Private Sub btnShowChildForm_Click(...) Handles btnShowChildForm.Click

        Dim frm As New Form2

        frm.ShowDialog()
        MsgBox(frm.txtUserInput.Text)
        frm.Dispose()

End Sub

Many thanks to all responses that you have made for my post... i really appreciate that...

Especially for Tom, your explanation and code works perfectly... many thanks to you...
 
Back
Top