Visual Basic 2012:将变量从一个表单传递到另一个表单

时间:2013-11-21 03:29:58

标签: vb.net showdialog

我有两种形式,主(Main.vb)程序窗口和程序启动时出现的弹出窗口(getInitialBalance.vb)。我需要从弹出窗口到主程序输入一个输入PopUp窗口的值。相关代码如下所示:

getinitialbalance.vb

Public Class GetInitialBalance
    Public initialBalance As Integer

    Private Sub btnApplyInitialBal_Click(sender As Object, e As EventArgs) Handles btnApplyInitialBal.Click
        Dim textinput As Integer = txtInitialBalance.Text
        initialBalance = textinput
        Me.Close()
    End Sub
End Class

Main.vb

Public Class Main
    Private Sub Main_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        GetInitialBalance.ShowDialog()
    End Sub

    Dim localInitialBalance As Integer = GetInitialBalance.initialBalance

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        MsgBox(localInitialBalance)
    End Sub
End Class

1 个答案:

答案 0 :(得分:2)

新建GetInitialBalance表单,然后当用户在弹出式对话框中点击OK时,从initialBalance的引用中获取值GetInitialBalance,如下所示:< / p>

Private Sub Main_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Dim popup As New GetInitialBalance
    If popup.ShowDialog = Windows.Forms.DialogResult.OK Then
        localInitialBalance = popup.initialBalance
    End If
End Sub

您的整个代码应如下所示:

Public Class GetInitialBalance
    Public initialBalance As Integer

    Private Sub btnApplyInitialBal_Click(sender As Object, e As EventArgs) Handles btnApplyInitialBal.Click
        initialBalance = Convert.ToInt32(textinput)
        Me.DialogResult = Windows.Forms.DialogResult.OK
    End Sub
End Class

Public Class Main
    Dim localInitialBalance As Integer

    Private Sub Main_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim popup As New GetInitialBalance
        If popup.ShowDialog = Windows.Forms.DialogResult.OK Then
            localInitialBalance = popup.initialBalance
        End If
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        MsgBox(localInitialBalance)
    End Sub
End Class