如何从另一个窗体在窗体中设置控件属性 - VB.net
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24607417/
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
How to set control properties in Form from another Form - VB.net
提问by
I have 2 Forms, Form1 and Form2. They each contain 1 Label and 1 Button.
我有 2 个表格,Form1 和 Form2。它们每个都包含 1 个标签和 1 个按钮。
How can I change Button Properties to Enabled=False or true and Label text to Label2.text="Text_Label1_From_Form1" in Form2 via Form1..?
如何通过 Form1.. 在 Form2 中将按钮属性更改为 Enabled=False 或 true 并将标签文本更改为 Label2.text="Text_Label1_From_Form1" ?
this is my code but nothing changed in Form2.
这是我的代码,但在 Form2 中没有任何变化。
Dim FrmM As New Form2
FrmM.Show()
FrmM.Label2.Text = Me.Label1.Text
FrmM.Button2.Enabled = False
someone pls help..?? thanks.
有人请帮忙.. ? 谢谢。
EDIT :
编辑 :
I want to try to clarify my question.
我想尝试澄清我的问题。
I have 2 forms.
我有2个表格。
FORM1
表格1
Label1.Text = "Fantastic!"
button1
FORM2
表格2
Label2.text = ""
When I click on Button1 then Label2 is in Form2 be Label2.text = "Fantastic!"
当我点击 Button1 然后 Label2 在 Form2 中是 Label2.text = "Fantastic!"
This perhaps could clarify my question .. thanks
这也许可以澄清我的问题..谢谢
回答by user3478160
Well, if i get your question straight you want to change a label text in a form through another form. Well, this is the code you are using, button1 in form1 (if the label in form2 named "label2":
好吧,如果我直截了当地回答您的问题,您想通过另一种形式更改一种形式中的标签文本。嗯,这是你正在使用的代码,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
Well, your code is alright and it's not wrong, but you might showed the form as form2 not as FrmW when you declare a variable as new form and edit it and want to show it you should write declaredvariable.show not form.show() Anyway, let's assume that you have two forms (Form1,Form2), And the label in form2 that you want to change called "label2" and you want to changed using form1 with a button, the only thing you will code inside is the button, you wont need anything else not any label in form1 nor coding in form2. So the code in form1 I just used to answer this question is:
好吧,你的代码没问题,没有错,但是当你将一个变量声明为新表单并编辑它并想要显示它时,你可能会将表单显示为 form2 而不是 FrmW无论如何,让我们假设您有两个表单(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
you wont need to change any other control or anything to change label2 in form2, the only thing you will need is to code inside the button that will change label2 in form2. PS: I'm using VS2012, PS: you dont need to deplicate form2 you can just do it directly, just like in the first code. Regrads.
您不需要更改任何其他控件或任何内容来更改 form2 中的 label2,您唯一需要的是在将更改 form2 中的 label2 的按钮内编写代码。PS:我用的是VS2012,PS:你不需要复制form2你可以直接做,就像在第一个代码中一样。重新毕业。

