Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing variable from one form to another in vb.net

I've looked this question up 10 times but each answer is too specific to the question.

I have two public classes, one per form.

The first form has a textbox and two buttons:

Public Class frmAdd
    Public addvar As String
    Public Sub UltraButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)  Handles btnAddNote.Click

        If txtAdd.Text = String.Empty Then
            MsgBox("Please complete the notes textbox!")
        Else
            addvar = txtAdd.Text
            MsgBox(addvar)
            Close()
        End If
    End Sub

    Public Sub UltraButton2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Me.Close()
    End Sub
End Class

In the second form I want to take that addvar variable and say

Public Sub saveButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles addButton.Click

    frmAdd.show()

    me.textbox1.text = addvar

How do I get this to work in vb.net?

like image 306
anm Avatar asked Feb 27 '14 19:02

anm


People also ask

How pass textbox value from one form to another in VB net?

In order to retrieve a control's value (e.g. TextBox. Text ) from another form, the best way is to create a module and create a property for the private variable. Then in the textbox's TextChanged event use the property getCustomerFirstNameSTR to hold the textbox's text.

How call a function from another form in VB net?

You'd call Main. Main() or just Main() . If the Module is nested in Form1 you'd have to call Form1.


1 Answers

You need to read the field out of the frmAdd value

Me.textbox1.Text = frmAdd.addvar

Note that this value won't be available until the form has completed, and is closed (Me.close). Hence you want to use ShowDialog (doesn't return until form is closed) vs. Show (which returns immediately after displaying the form).

frmAdd.ShowDialog()
Me.textbox1.Text = frmAdd.addvar
like image 148
JaredPar Avatar answered Nov 05 '22 10:11

JaredPar