vb中窗体间传值问题

来源:百度知道 编辑:UC知道 时间:2024/07/02 20:55:43
form1中的代码:
Option Explicit

Public a, b, c As Integer

Private Sub Command1_Click() '按钮
Form2.Show '调用窗体form2运算c的值
Print "a+b=" & c
End Sub

Private Sub Form_Click()
Print "a=" & a
Print "b=" & b
End Sub

Private Sub Form_Load()
a = 1
b = 2
End Sub

form2窗体中的代码:
Option Explicit

Private Sub Form_KeyPress(KeyAscii As Integer)
If KeyAscii = 13 Then
'关闭form2语句怎么写,不是hide
End If
End Sub

Private Sub Form_Load()
Form1.c = Form1.a + Form1.b
End Sub

问题1:运行后就是不对
总是2:运行窗体form2按回车用什么方法关闭窗体2
这两个窗体,最主要的目的就是,让窗体2中得到数据,然后直接在1窗体中调用。

当你第一次单击Commandbutton时c这个值并未赋值,直接就显示form2了但并没有直接执行form2中的load事件,就在form1上直接显示“a+b”

关闭form2 unload form2

建议修改:
Private Sub Command1_Click() '按钮
Form2.Show 1'调用窗体form2运算c的值
Print "a+b=" & c
End Sub

运行窗体form2按回车关闭窗体2,
form2窗体中的代码:
Option Explicit

Private Sub Form_KeyPress(KeyAscii As Integer)
If KeyAscii = 13 Then
Unload me
End If
End Sub
把Print换了吧,msgbox,运行后没问题

多窗体调用变量建议在模块中定义变量!
新建个模块在模块中定义A,B,C
Private Sub Command1_Click() '按钮
Form2.Show '调用窗体form2运算c的值
Print "a+b=" & c '这里所有代码都会执行完,如果没猜错的话应该会输出0
End Sub

Private Sub Form_Click()
Print "a=" & a
Print "b=" & b
End Sub

Private Sub Form_Load()
a = 1
b = 2
End Sub

form2窗体中的代码:
Option Explicit

Private Sub Form_KeyPress(KeyAscii As Integer)
If KeyAscii = 13 Then
UNLOAD