在 vb.net 中将变量从一种形式传递到另一种形式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22078219/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 17:26:56  来源:igfitidea点击:

Passing variable from one form to another in vb.net

vb.netvisual-studio-2010

提问by anm

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

我已经将这个问题看了 10 次,但每个答案都太针对问题了。

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

在第二种形式中,我想采用那个 addvar 变量并说

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?

我如何让它在 vb.net 中工作?

采纳答案by JaredPar

You need to read the field out of the frmAddvalue

您需要从frmAdd值中读取字段

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).

请注意,在表单完成并关闭之前,此值将不可用。因此,您要使用ShowDialog(在表单关闭之前不返回)与Show(在显示表单后立即返回)。

frmAdd.ShowDialog()
Me.textbox1.Tex = frmAdd.addvar