在 C# 中以另一种形式更改标签的文本?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10704020/
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
Changing a label's text in another form in C#?
提问by Hunter Mitchell
I have a label called LabelX1. This is on form2. On form1, i have a button. I want the button's text to be transferred to the other form's label. I have tried
我有一个名为 LabelX1 的标签。这是在form2上。在form1 上,我有一个按钮。我希望按钮的文本被转移到另一个表单的标签。我试过了
form2 frm2 = new form2();
frm2.labelX1.Text = this.button1.text;
But it does not work. Is there an easy, straight forward way of doing this?
但它不起作用。有没有一种简单直接的方法来做到这一点?
采纳答案by Davio
You need to expose your label or its property.
您需要公开您的标签或其属性。
In form 2:
在表格 2 中:
public string LabelText
{
get
{
return this.labelX1.Text;
}
set
{
this.labelX1.Text = value;
}
}
Then you can do:
然后你可以这样做:
form2 frm2 = new form2();
frm2.LabelText = this.button1.text;
回答by hawk
Is there an easy, straight forward way of doing this?
有没有一种简单直接的方法来做到这一点?
Easiest way is to make labelX1 a public member of form2. The issue you're having is because from Form1 code form2.labelX1 isn't visible. In form2 designer you can go to properties of labelX1 and set it's visibility to public/internal.
最简单的方法是让 labelX1 成为 form2 的公共成员。您遇到的问题是因为从 Form1 代码 form2.labelX1 不可见。在 form2 设计器中,您可以转到 labelX1 的属性并将其可见性设置为公共/内部。
Better approach would be to expose labelX1.Text as a property which can be set in code outside the class.
更好的方法是将 labelX1.Text 作为可以在类外的代码中设置的属性公开。
回答by Val Bakhtin
Do you have exception? You can make public property on form2, with setter to set text on the label, or make labex1 access modifier public and set it directly. It should work.
你有例外吗?您可以在 form2 上设置 public 属性,使用 setter 在标签上设置文本,或者将 labex1 访问修饰符设置为 public 并直接设置它。它应该工作。
回答by Micah Armantrout
You can make labelX1public, and it will work, but there is a better way to do this:
你可以labelX1公开,它会起作用,但有一个更好的方法来做到这一点:
http://www.codeproject.com/Articles/14122/Passing-Data-Between-Forms
http://www.codeproject.com/Articles/14122/Passing-Data-Between-Forms
回答by Eric Dahlvang
You could modify the constructor of Form2 like this:
您可以像这样修改 Form2 的构造函数:
public Form2(string labelText)
{
InitializeComponent();
this.labelX1.Text = labelText;
}
then create Form2 passing in the text:
然后创建传入文本的 Form2:
Form2 frm2 = new Form2(this.button1.text);
回答by General Grey
inside form2 write this
在form2里面写这个
public void ChangeLabel(string s)
{
labelX1.Text = s;
}
then where you create Form 2 do this
然后在创建 Form 2 的地方执行此操作
form2 frm2 = new form2();
frm2.ChangeLabel(this.button1.text);
回答by banging
Or you can do this >>
或者你可以这样做>>
((Label)frm2.Controls["labelX1"]).Text = "test";
回答by Amit Gohel
I changed my parent window property to the following code:
我将父窗口属性更改为以下代码:
this.MdiParent.Controls["label1"].Text = "test";
回答by Hstm91
form2 frm2 = new form2();
((Label)frm2.Controls["labelX1"]).Text=button1.Text;
frm2.Show();
回答by Joe Ruder
If you are needing to access the form2 from elsewhere in your code (like a button press for instance) you will not be able to see the instance of the form you create. To solve that I create a public instance to hold a reference to it like:
如果您需要从代码中的其他地方访问 form2(例如按下按钮),您将无法看到您创建的表单实例。为了解决这个问题,我创建了一个公共实例来保存对它的引用,例如:
public form2 form2_pub;
Then after you create it you assign the new one to your public instance:
然后在创建它之后,将新实例分配给您的公共实例:
form2 frm2 = new form2();
frm2.Show();
form2_pub = frm2
Now you can reference form2_pub throughout your routines.
现在,您可以在整个例程中引用 form2_pub。
Works for me at least.
至少对我有用。
Remember, in your setter you can run whatever other code you want. For instance, I use the following to show what I want on another form by just setting show_scanning to true:
请记住,在您的 setter 中,您可以运行任何您想要的其他代码。例如,我使用以下内容通过将 show_scanning 设置为 true 来在另一个表单上显示我想要的内容:
public bool show_scanning //turns on the scanning screen
{
set
{
scanning_pnl.Visible = true;
notReady_pnl.Visible = false;
timer1.Enabled = true;
}
}

