访问新表单上的控件

时间:2022-06-01 16:39:49

I have 2 forms in my project, form1 and form2. When I click in a button in form1 I run this code:

我的项目中有2个表单,form1和form2。当我单击form1中的按钮时,我运行此代码:

Form tempform = new Form2();
tempform.Show();

In my code for Form2 I have a label which I now need to change the text. How can I access the label?

在我的Form2代码中,我有一个标签,我现在需要更改文本。我如何访问标签?

I tried:

tempform.label1.value = "new text"

And that didn't work, I even tried to access using the Controls collection but I think I messed that up. Is there any way I can access the label? OR is there any way I can pass a value to that new form and then have that form alter the label text.

这不起作用,我甚至尝试使用Controls集合进行访问,但我认为我搞砸了。有什么方法可以访问标签吗?或者有什么方法可以将值传递给新表单,然后让该表单更改标签文本。

Thanks

2 个解决方案

#1


If the label value should only be set once, when the form is created, then use a constructor for Form2 like this:

如果标签值只应设置一次,那么在创建表单时,请使用Form2的构造函数,如下所示:

public Form2(string labelValue)
{
  _labelValue = labelValue;
}

and then call that constructor when you create the form.

然后在创建表单时调用该构造函数。

Alternatively, if the label changes over the lifetime of the form, make a public property:

或者,如果标签在表单的生命周期内发生更改,请创建公共属性:

public string LabelValue
{
  get { return label1.Text; }
  set { label1.Text = value; }
}

Also I would recommend naming the parameters and/or properties to reflect the meaning of the value, for example "titleText" instead of "labelValue". That way Form2 can decide how it wants to display the information (in the title bar, a label, a textbox, etc), and Form1 doesn't have to worry about that.

另外,我建议命名参数和/或属性以反映值的含义,例如“titleText”而不是“labelValue”。这样,Form2可以决定它如何显示信息(在标题栏,标签,文本框等),Form1不必担心这一点。

Edit: Consume the LabelValue property like this:

编辑:像这样使用LabelValue属性:

Form2 newForm = new Form2(); // Assign object to a Form2 instead of Form
newForm.LabelValue = "new text";
newForm.Show();

#2


Controls have protected access by default. You can change that to public, or you can add a method/property to your form2 class to set the label and call that (latter method is generally preferred to preserve encapsulation and because the designer may want to overwrite your public change.).

控件默认具有受保护的访问权限您可以将其更改为public,或者您可以向form2类添加方法/属性以设置标签并调用它(后一种方法通常首选保留封装,因为设计人员可能希望覆盖您的公共更改。)。

#1


If the label value should only be set once, when the form is created, then use a constructor for Form2 like this:

如果标签值只应设置一次,那么在创建表单时,请使用Form2的构造函数,如下所示:

public Form2(string labelValue)
{
  _labelValue = labelValue;
}

and then call that constructor when you create the form.

然后在创建表单时调用该构造函数。

Alternatively, if the label changes over the lifetime of the form, make a public property:

或者,如果标签在表单的生命周期内发生更改,请创建公共属性:

public string LabelValue
{
  get { return label1.Text; }
  set { label1.Text = value; }
}

Also I would recommend naming the parameters and/or properties to reflect the meaning of the value, for example "titleText" instead of "labelValue". That way Form2 can decide how it wants to display the information (in the title bar, a label, a textbox, etc), and Form1 doesn't have to worry about that.

另外,我建议命名参数和/或属性以反映值的含义,例如“titleText”而不是“labelValue”。这样,Form2可以决定它如何显示信息(在标题栏,标签,文本框等),Form1不必担心这一点。

Edit: Consume the LabelValue property like this:

编辑:像这样使用LabelValue属性:

Form2 newForm = new Form2(); // Assign object to a Form2 instead of Form
newForm.LabelValue = "new text";
newForm.Show();

#2


Controls have protected access by default. You can change that to public, or you can add a method/property to your form2 class to set the label and call that (latter method is generally preferred to preserve encapsulation and because the designer may want to overwrite your public change.).

控件默认具有受保护的访问权限您可以将其更改为public,或者您可以向form2类添加方法/属性以设置标签并调用它(后一种方法通常首选保留封装,因为设计人员可能希望覆盖您的公共更改。)。