从Form2传递简单整数到Form1

时间:2012-11-14 05:35:46

标签: vb.net

试图弄清楚如何将一个简单的整数(StartingTeam)从Form2传递给Form1。

表格2代码

Public Class frmTeamChoose
Public StartingTeam As Integer
Public Sub btnTeam1_Click(sender As Object, e As EventArgs) Handles btnTeam1.Click
    StartingTeam = 1
End Sub

Public Sub btnTeam2_Click(sender As Object, e As EventArgs) Handles btnTeam2.Click
    StartingTeam = 2
End Sub

End Class

表单1称为Form1

1 个答案:

答案 0 :(得分:1)

虽然您需要提供更多信息,但我会尽力帮助您:
我想你已经从Form1打开了Form2:

'In form1:
Dim k as integer=Form2.StartingTeam 

改进的解决方案是创建属性:
表格2代码:

 Private miStartingTeam 
 Public Property StartingTeam As Integer
    Get
        Return miStartingTeam 
    End Get
    Set(ByVal value As Integer)
        miStartingTeam = value
    End Set
 End Property

然后你的代码就是这样 在Form1中:

'Open Form2
Dim f2 as new Form2
'f2.StartingTeam=1 'if you want to set a value before f2 opening
f2.Show


'Get StartingTeam from f2
dim k as integer=f2.StartingTeam 

如果您还有其他需要,请告诉我