传递给另一个表单,VB.NET 的变量值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14427695/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Variable Value Passing to another Form, VB.NET
提问by Minima
I do have Two Public Variables, each are from two different forms..
我确实有两个公共变量,每个变量都来自两种不同的形式。
Form1.VB
Public UserNo As String
Form2.VB
Public MyUserNo As String
On my Form2.VB File, I assign value to the UserNo of Form1.VB
在我的 Form2.VB 文件中,我为 Form1.VB 的 UserNo 赋值
Form1.UserNo = MyUserNo
Whenever I access the Form1.VB, the Value of the MyUserNo got empty, What should I do? Both Forms are not closed.
每当我访问 Form1.VB 时,MyUserNo 的值都为空,我该怎么办?两种形式都没有关闭。
I also tried to re-assign the value when I need to use it on the Form1.VB
当我需要在 Form1.VB 上使用它时,我也尝试重新分配该值
UserNo = Form2.MyUserNo
回答by Arun Sharma
First Correct is Syntax is:
首先正确的是语法是:
Form1.VB
Public UserNo As String
Form2.VB
Public MyUserNo As String
In Form1
UserNo=Form2.MyUserNo
Second Thing: First you should store some value in MyUserNo before storing it into UserNo. That's why you are getting empty value.
第二件事:首先,您应该先在 MyUserNo 中存储一些值,然后再将其存储到 UserNo 中。这就是为什么你得到空值。
回答by Kiran1016
Make the variable static/Shared and try again,it should work.
使变量静态/共享并重试,它应该可以工作。
回答by Joel Coehoorn
You can have more than one instance of a form, you know. Forms are objects, just like anything else. You need a variable in each form to hold a reference to the instance of each form that you are using.
您知道,您可以拥有多个表单实例。表单是对象,就像其他任何东西一样。您需要在每个表单中都有一个变量来保存对您正在使用的每个表单的实例的引用。
回答by Bilal Mrad
If you don't call InitializeComponent(), your complete GUI is not going to render.
如果不调用 InitializeComponent(),则不会呈现完整的 GUI。
... InitializeComponent() Form1.UserNo = MyUserNo ...
... InitializeComponent() Form1.UserNo = MyUserNo ...
回答by Camel Share
Try this:
尝试这个:
[Form1.UserNo = form2.MyUserNo]
回答by Robert Cotton
What you need to do is create your variables in a module as private, then generate some assessors for them.
您需要做的是在模块中创建私有变量,然后为它们生成一些评估器。
Example:
例子:
Module modVariables
Private strUserNoSTR as String = New String(String.Empty)
Public Property getUserNoSTR() As String
Get
Return strUserNoSTR
End Get
Set(ByVal strUserNo As String)
strUserNoSTR = strUserNo
End Set
End Property
Private strMyUserNoSTR As String = New String(String.Empty)
Public Property getMyUserNoSTR As String
Get
Return strMyUserNoSTR
End Get
Set(ByVal strMyUserNo As String)
strMyUserNoSTR = strMyUserNo
End Set
End Property
End Module
After you generate the getter and setter public methods you can notice that your two private variables are within them, when you create the variables they are accessible from any form.
在生成 getter 和 setter 公共方法后,您会注意到您的两个私有变量在其中,当您创建变量时,它们可以从任何形式访问。
The reason why you keep losing the variables value is because when you try to access its value from another form (basically you're calling it from another class) the compiler has to create a new instance of that variable and when that happened the variable is set back to its original value which is of type empty string
. Calling them from a module keeps them from being re-instantiated.
您不断丢失变量值的原因是因为当您尝试从另一种形式(基本上是从另一个类调用它)访问它的值时,编译器必须创建该变量的新实例,当发生这种情况时,该变量是设置回其类型的原始值empty string
。从模块调用它们可以防止它们被重新实例化。
How To Use Them:
如何使用它们:
To get the value of strMyUserNo you call the getter of strMyUserNoSTR:
要获取 strMyUserNo 的值,请调用 strMyUserNoSTR 的 getter:
TextBox.Text = getMyUserNoSTR
To set the value of strMyUserNoSTR:
设置 strMyUserNoSTR 的值:
getMyUserNoSTR = someValuePlacedInThisVariable 'This sets it's value.
TextBox.Text = getMyUserNoSTR 'Now it's value is someValuePlacedInThisVariable.
回答by maniek207
it work
add it to form what you want value in next form
它可以
添加它以在下一个表单中形成您想要的值
Function getvariable()
Return (VARIABLE)
End Function
In next form to get VARIABLE
在下一个形式中获得 VARIABLE
VARIABLE2 = Form.getvariable()
回答by Suong Tran
Use variable value as Public
使用变量值作为公共
For Example
In Form1:
例如
在 Form1 中:
Public Str1 as String
So in Form2 you can use:
所以在 Form2 中你可以使用:
Str2=Form1.Str1