1个变量 - 2个表格 [英] 1 Variable - 2 Forms
问题描述
我正在敲打着我认为我不理解的东西
我在帮助中读到的内容。
它必须如此简单但我不能理解弄明白我在读什么。 (bb
2003)
假设我有Form1和Form2。
每个表格都是空白的。 (没有文字框或任何东西)
非常简单示例:(我只需要一个开始,然后我可以修改它和
从中学习做我需要做的事情)
在form1_Load
''我有一个名为cHello的变量
Dim cHello As String =" Hello World"
''我打电话给表格。
Dim frmSecondForm As Form = New Form2()
frmSecondForm .ShowDialog()
EndSub
在表单2'的加载中,我想
MsgBox(cHello,MsgBoxStyle.OKOnly, Box Of Box)
close()
EndSub
如何正确地将变量传递给form2?
谢谢,
Miro
Miro napisal(a):
我正在敲打我认为我不理解的东西
what我正在阅读帮助。
它必须如此简单,但我无法弄清楚我正在读什么。 (bb
2003)
假设我有Form1和Form2。
每个表格都是空白的。 (没有文字框或任何东西)
非常简单示例:(我只需要一个开始,然后我可以修改它和
从中学习做我需要做的事情)
在form1_Load
''我有一个名为cHello的变量
Dim cHello As String =" Hello World"
''我打电话给表格。
Dim frmSecondForm As Form = New Form2()
frmSecondForm .ShowDialog()
EndSub
在表单2'的加载中,我想
MsgBox(cHello,MsgBoxStyle.OKOnly, Box Of Title)
close()
EndSub
如何正确地将变量传递给form2?
谢谢,
Miro
你好Miro,
如果你想在多个表格之间传递变量,你必须声明
它为公共:
''form1
Public _text As String
Pr ivate Sub Form1_Load(ByVal sender As System.Object,ByVal e As
System.EventArgs)处理MyBase.Load
_text =" Hello"
Dim f2 As New Form2
Form2.Show()
End Sub
''form2
Private Sub Form2_Load(ByVal sender As System.Object,ByVal e As
System.EventArgs)处理MyBase.Load
Dim TextFromForm1 As String
TextFromForm1 = Form1._text
MessageBox.Show(TextFromForm1)
End Sub
在使用vb.net时,你必须知道这里的一切都是
对象。所以如果想要访问对象属性,方法......你必须
指定这个对象,就像在这里完成一样:
Form1._text
如果您在处理多个表格时遇到问题,请阅读以下文章:
http://www.devcity.net/Articles/94/multipleforms.aspx
http://www.devcity.net/Articles/100/multipleforms2.aspx
http://www.devcity.net/Articles/102/multipleforms3.aspx
http://www.devcity.net/Articles/117 / ... pleforms4.aspx
顺便说一句,msgbox()是clasic vb函数。在vb.net中,建议使用
:
messagebox.show(文字,标题,按钮,图标)
希望这会有所帮助。
Regardsm
sweet_dreams
在Form2 thru上传递它构造函数参数wen调用form2
伪代码:
dim myString as string =" hello world"
dim f2 as new form2(myString)
-----现在形式2
dim myvar as string
sub new(byval x as string)
myvar = x
end sub
现在加载事件把msgbox n显示myvar
你也可以把一个变量设为Public所以一切都能看到它,(VB 2005)ex:
Public str As String
Private Sub Form1_Load(..)
Dim frm As新表格2
str =" Hello"
frm.Show()
E nd Sub
'''''表2
Private Sub Form2_Load(...)
me.text = My.Forms.Form1.str
end Sub
'''''''''''''''' '
或者你可以让变量反映出来'
form1上的一个属性
只需2美分,希望这有帮助
-
-iwdu15
I am banging my head around something that I think I just dont understand
what I am reading in the helps.
Its gotta be so simple but I just cant figure out what im reading. ( vb
2003 )
Lets say i have Form1 and Form2.
Each form is blank. ( no text boxes or anything )
Very Simple Example: ( i just need a start and then i can modify it and
learn from it to do what i need to do )
On form1_Load
''I have a variable called cHello
Dim cHello As String = "Hello World"
'' I call form two.
Dim frmSecondForm As Form = New Form2()
frmSecondForm .ShowDialog()
EndSub
On form 2''s load, I want to
MsgBox( cHello ,MsgBoxStyle.OKOnly, "Title Of Box")
close()
EndSub
How do I Properly pass the variable to form2 ?
Thanks,
Miro
Miro napisal(a):I am banging my head around something that I think I just dont understand
what I am reading in the helps.
Its gotta be so simple but I just cant figure out what im reading. ( vb
2003 )
Lets say i have Form1 and Form2.
Each form is blank. ( no text boxes or anything )
Very Simple Example: ( i just need a start and then i can modify it and
learn from it to do what i need to do )
On form1_Load
''I have a variable called cHello
Dim cHello As String = "Hello World"
'' I call form two.
Dim frmSecondForm As Form = New Form2()
frmSecondForm .ShowDialog()
EndSub
On form 2''s load, I want to
MsgBox( cHello ,MsgBoxStyle.OKOnly, "Title Of Box")
close()
EndSub
How do I Properly pass the variable to form2 ?
Thanks,
MiroHi Miro,
If you want to pass variable between multiple form you have to declare
it as Public:
''form1
Public _text As String
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
_text = "Hello"
Dim f2 As New Form2
Form2.Show()
End Sub
''form2
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim TextFromForm1 As String
TextFromForm1 = Form1._text
MessageBox.Show(TextFromForm1)
End Sub
While working with vb.net you have to be aware that everything is
object here. So if want access objects property, method... you have to
specify this object like it was done here:
Form1._text
If you have problems working with multiple forms read this articles:
http://www.devcity.net/Articles/94/multipleforms.aspx
http://www.devcity.net/Articles/100/multipleforms2.aspx
http://www.devcity.net/Articles/102/multipleforms3.aspx
http://www.devcity.net/Articles/117/...pleforms4.aspx
And by the way, msgbox() is clasic vb function. In vb.net it is
recommended to use:
messagebox.show(text, caption, buttons, icon)
Hope this helps.
Regardsm
sweet_dreams
pass it on Form2 thru constructor parameter wen calling form2
pseudo code:
dim myString as string="hello world"
dim f2 as new form2(myString)
----- now in form 2
dim myvar as string
sub new (byval x as string)
myvar=x
end sub
now on load event put the msgbox n display myvar
you could also make one variable Public so everything can see it, (VB 2005) ex:
Public str As String
Private Sub Form1_Load(..)
Dim frm As New Form2
str = "Hello"
frm.Show()
End Sub
''''''form 2
Private Sub Form2_Load(...)
me.text = My.Forms.Form1.str
end Sub
''''''''''''''''''''''''''''''''''''''''''''''''
or you could make the variable reflect a property on form1
just my 2 cents, hope this helps
--
-iwdu15
这篇关于1个变量 - 2个表格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!