如何从另一个窗体 - VB.net中设置窗体中的控件属性

时间:2014-07-07 09:35:51

标签: vb.net

我有2个表单,Form1和Form2。它们每个都包含1个Label和1个Button。

如何将Button Properties更改为Enabled = False或true并将文本标记为Label2.text =" Text_Label1_From_Form1"在Form2中通过Form1 ..?

这是我的代码,但在Form2中没有任何改变。

Dim FrmM As New Form2
FrmM.Show()
FrmM.Label2.Text = Me.Label1.Text
FrmM.Button2.Enabled = False
有人请帮忙.. ??感谢。

编辑:

我想试着澄清我的问题。

我有两种形式。

FORM1

Label1.Text = "Fantastic!"

button1

FORM2

Label2.text = ""

当我点击Button1时,Label2在Form2中是Label2.text ="太棒了!"

这也许可以澄清我的问题..谢谢

1 个答案:

答案 0 :(得分:0)

好吧,如果我直接提出你的问题,你想通过另一种形式更改表格中的标签文字。 好吧,这是你正在使用的代码,form1中的button1(如果form2中的标签名为“label2”:

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
       Form2.label2.text = "Fantastic!" ' Change label2 text in form2 to "fantastic!"
    End Sub

嗯,你的代码没问题,也没错,但是当你将一个变量声明为新形式并编辑它并想要显示它时你可能会将表单显示为form2而不是FrmW declaredvariable.show not form.show() 无论如何,让我们假设您有两个表单(Form1,Form2),并且您想要更改的form2中的标签名为“label2”,并且您希望使用带有按钮的form1进行更改,您唯一要编写的内容就是按钮,你不需要form1中的任何标签,也不需要在form2中编码。因此我刚才用来回答这个问题的form1中的代码是:

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim frm As New Form2 ' Declaring frm as a deplicate of Form2
    frm.Show() ' Showing the frm (the deplicated version of Form2)
    frm.Label2.Text = "Fantastic!" 'Changing label2 text in frm to "Fantastic!"
    End Sub

您不需要更改任何其他控件或任何更改form2中的label2,您唯一需要的是在将更改form2中的label2的按钮内部进行编码。 PS:我正在使用VS2012,PS:您不需要简化form2,您可以直接执行,就像在第一个代码中一样。 Regrads。

相关问题