我用vb做了一个多重窗体显示程序。

来源:百度知道 编辑:UC知道 时间:2024/04/28 15:28:57
但是,这两个窗口,从一个窗口上得到的变量在另外一个窗口上显示为0,没有赋值。这是怎么一回事。我可是按照书本上的做的。

第一种方法:定义全局变量,然后在各个窗体中直接使用,例如
Public strCustomerID As String
Public strCustomerName As String

第二种方法:添加一个模块专门用于定义变量,然后定义一些Public过程或函数来设置和获取这些数据,例如
Dim strCustomerID As String
Dim strCustomerName As String

Public Sub SetCustomerID(CustomerID As String)
strCustomerID = CustomerID
End Sub

Public Function GetCustomerID() As String
GetCustomerID = strCustomerID
End Function

Public Sub SetCustomerName(CustomerName As String)
strCustomerName = CustomerName
End Sub

Public Function GetCustomerName() As String
GetCustomerName = strCustomerName
End Function

简单一点的办法,两个窗口间用一个控件做为传递变量的媒介

假定你有一个变量a,并有两个FORM,FORM1和FORM2,a在Form1中,现在要传递到Form2,使用一个文本框做为传递媒介,下面如此解决
form1中
text1.text=str(a)

form2中
print form1.text1.text

当然也可以用类似方法在FORM2中使用控件,FORM1中的文本框如果不喜欢可以把它的visible属性设置为Flase