参考代码如下:
'VB中,多个窗体件返回值
'本例子中两个窗体Form1和Form2
'Form1中:添加Text1和Text2两个文本框,一个按钮Command1
'Form2中:添加Text1和Text2两个文本框,两个按钮Command1和Command2
'Form1代码:
Private WithEvents frm2 As Form2
Private Sub Command1_Click()
If frm2 Is Nothing Then
Set frm2 = New Form2
frm2.Show
End If
End Sub
Private Sub Form_Load()
Command1.Caption = "打开Form2"
End Sub
Private Sub Form_Unload(Cancel As Integer)
If Not frm2 Is Nothing Then
Unload frm2
End If
Set frm2 = Nothing
End Sub
Private Sub frm2_ExitForm() '关闭Form2时
Unload frm2
Set frm2 = Nothing
End Sub
Private Sub frm2_ReturnValue(T As String, V As String) '有返回值时
Controls(T).Text = V
End Sub
'Form2代码:
Public Event ReturnValue(T As String, V As String)
Public Event ExitForm()
Private Sub Command1_Click()
RaiseEvent ReturnValue("Text1", Text1.Text)
End Sub
Private Sub Command2_Click()
RaiseEvent ReturnValue("Text2", Text2.Text)
End Sub
Private Sub Form_Load()
Command1.Caption = "执行返回Text1的值"
Command2.Caption = "执行返回Text2的值"
End Sub
Private Sub Form_Unload(Cancel As Integer)
RaiseEvent ExitForm
End Sub