在form1和form2之间传递Vb.net变量 [英] Vb.net variable pass between form1 and form2
问题描述
你好我有2个表单,有变量,我想从Form1发送变量到Form2,但我没有这样做,我的代码在下面,你能帮帮我吗?
Form1有2个文本框和1个按钮,For Form2我有1个标签和1个按钮。我需要将d和b值发送到Form2标签1
Form1代码:
Private Sub Button1_Click(ByVal sender As System。对象,ByVal e As System.EventArgs)处理Button1.Click
调暗为十进制
Dim b为十进制
Dim c As Decimal
Dim d As Decimal
a = Val(TextBox1.Text)
b = Val(TextBox2.Text)
c = a + b * 5/8
d = c / 3
MessageBox.Show(d)
Form2.Show()
End Sub
form2;
Private Sub Button1_Click(ByVal sender As System.Object,ByVal e As System.EventArgs)处理Button1.Click
Label1.Text = d + b
End Sub
我尝试过的事情:
看到我上面的路径,但我不能实现在表单之间传递变量
在Form2上创建一个属性,保存您希望传递给私有变量的值,在显示表单之前调用该属性。 br />
Fr示例下面的Form1会说Form2.Setb = b来设置b值。
< pre lang =vb> 公共 类 Form2
私有 b 作为 十进制
私人 d 作为 十进制
公共 WriteOnly 属性 Setb 作为 十进制
设置(值 As 十进制)
b =值
结束 设置
结束 属性
公开 WriteOnly 属性已设置为 十进制
设置(值作为 < span class =code-keyword> Decimal )
d = val ue
结束 设置
结束 属性
私有 Sub Button1_Click( ByVal sender As System。对象, ByVal e 作为 System.EventArgs)句柄 Button1.Click
Label1.Text =(d + b).ToString
End Sub
或者在Form2上创建一个公共全局变量直接设置。
公共 类 Form2
公共 b 作为 十进制 ' 你可以通过Form2.b =?
公共 d 作为 <从Form1访问这些span class =code-keyword>十进制
私有 Sub Button1_Click( ByVal sender As System。 Object , ByVal e As System.EventArgs) Handles Button1.Click
Label1.Text =(d + b).ToString
End Sub
有多种方法可以在表单之间传递数据,读取在表单之间传递数据 [ ^ ]
这将是最简单的(但对我来说不是最好的)方式:
On 表单 1 :
私有 Sub Button1_Click( ByVal sender As System 。对象, ByVal e 作为系统。 EventArgs)句柄 Button1.Click
Form2.Label1.Text =(d + b).tostring
< span class =code-keyword>结束 Sub
最好的方法是使用属性......
Hello I have 2 Form, that have variables, I would like to send variables from Form1 to Form2,but I did not do this, my codes are below, Could you please help me out;
I have 2 textbox and 1 button for Form1 , For Form2 I have 1 label and 1 button.I need to send d and b values to Form2 label1
Form1 Codes:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim a As Decimal
Dim b As Decimal
Dim c As Decimal
Dim d As Decimal
a = Val(TextBox1.Text)
b = Val(TextBox2.Text)
c = a + b * 5 / 8
d = c / 3
MessageBox.Show(d)
Form2.Show()
End Sub
form2;
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Label1.Text = d + b
End Sub
What I have tried:
see my trails above but I cannot achieve to pass variables between forms
Create a property on Form2 that saves the value you wish to pass into a private variable, call the property before showing the form.
Fr the example below Form1 would say Form2.Setb = b to set the b value.
Public Class Form2 Private b As Decimal Private d As Decimal Public WriteOnly Property Setb As Decimal Set(value As Decimal) b = value End Set End Property Public WriteOnly Property Setd As Decimal Set(value As Decimal) d = value End Set End Property Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Label1.Text = (d + b).ToString End Sub
Alternatively create a public global variable on Form2 and set it directly.
Public Class Form2 Public b As Decimal ' You can access these from Form1 by Form2.b = ? Public d As Decimal Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Label1.Text = (d + b).ToString End Sub
There are a number of ways to pass data between forms, read Passing Data Between Forms[^]
That would be the easiest (but for me not the best) way :
On Form 1 : Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Form2.Label1.Text = (d + b).tostring End Sub
The best way would be to work with properties ...
这篇关于在form1和form2之间传递Vb.net变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!