Passing variable from one form to another in vb.net

試著忘記壹切 提交于 2020-01-01 11:39:10

问题


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?


回答1:


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 completes and is closed. 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.Tex = frmAdd.addvar


来源:https://stackoverflow.com/questions/22078219/passing-variable-from-one-form-to-another-in-vb-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!