C#:如何从另一个类更改 form1 中的标签文本?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/740890/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 23:20:57  来源:igfitidea点击:

C#: How can i change the text of a label thats in form1 from another class?

c#visual-studio-2008

提问by Nyight

im trying to change the text of a label from a class but have been unsuccessful.

我试图从一个类中更改标签的文本,但没有成功。

what i have is that im updating a variable in the class using get{} and set{}. i understand that i need to put or do something within set{} to get it to send the update value from the class back into the form1 class so that it can update the label.

我所拥有的是我使用 get{} 和 set{} 更新类中的变量。我知道我需要在 set{} 中放置或执行某些操作以使其将类中的更新值发送回 form1 类,以便它可以更新标签。

could anyone tell me how i could accomplish this? or if there is a way to update the label from within the class, that would be even better.

谁能告诉我我怎么能做到这一点?或者如果有一种方法可以从类中更新标签,那就更好了。

i hope this makes sense and thanks in advance.

我希望这是有道理的,并提前致谢。

采纳答案by Student for Life

You might want to consider using delegates or events and having your classes raise events back to the form, that way your classes will have no knowledge of your form.

您可能需要考虑使用委托或事件,并让您的类将事件引发回表单,这样您的类将不知道您的表单。

EXAMPLE

例子

class SomeClass
{
    public delegate void UpdateLabel(string value);

    public event UpdateLabel OnLabelUpdate;

    public void Process()
    {
        if (OnLabelUpdate != null)
        {
            OnLabelUpdate("hello");
        }
    }
}

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void UpdateLabelButton_Click(object sender, EventArgs e)
    {
        SomeClass updater = new SomeClass();
        updater.OnLabelUpdate += new SomeClass.UpdateLabel(updater_OnLabelUpdate);
        updater.Process();
    }

    void updater_OnLabelUpdate(string value)
    {
        this.LabelToUpdateLabel.Text = value;
    }
}

回答by John Saunders

If I understand your question, you have a form class, "form1". An instance of that class is being displayed. That form has a label on it. You are running a method in a different class. You want that method to update the label that's on form1.

如果我理解你的问题,你有一个表单类,“form1”。正在显示该类的一个实例。那个表格上有一个标签。您正在不同的类中运行方法。您希望该方法更新 form1 上的标签。

If that is correct, then, this is just like any other situation where a method needs to change a property of another class instance. Your method in the other class needs a reference to the form instance. When you call your method, instead of doing this:

如果这是正确的,那么这就像方法需要更改另一个类实例的属性的任何其他情况一样。您在另一个类中的方法需要对表单实例的引用。当你调用你的方法时,而不是这样做:

OtherClass oc = new OtherClass();
oc.OtherMethod();

do this:

做这个:

OtherClass oc = new OtherClass();
oc.OtherMethod(this);

Change the definition of Othermethod:

更改 Othermethod 的定义:

public void Othermethod(form1 theForm) {
    theForm.TheLabel.Text = "New Text!";
}

Then everything should be happy!

那么一切都应该是幸福的!

回答by Samuel

Since you haven't posted your code, I cannot tell why it's not working. But what you want is easily done.

由于您还没有发布您的代码,我不知道为什么它不起作用。但是你想要的很容易做到。

public class FormA : Form
{
  // ...

  public string Label1Value
  {
    get { return this.label1.Text; }
    set { this.label1.Text = value; }
  }
  // ...
}

And you can easily use it in any other form or code (except when it's in another thread.)

并且您可以轻松地以任何其他形式或代码使用它(除非它在另一个线程中。)

public class FormB : Form
{
  private void Button1_Click(object sender, MouseEventArgs e)
  {
    formA.Label1Value = "FormB was clicked";
  }
}


Update

更新

If you want to use events like Davide suggested, you could do something like this.

如果你想使用 Davide 建议的事件,你可以做这样的事情。

public class EULAEventArgs : EventArgs
{
  public string Signature { get; set; }
}

public class FormB : Form
{
  public event EventHandler<EULAEventArgs> EULAAccepted;
  protected virtual void OnEULAAccepted(EULAEventArgs e)
  {
    if (EULAAccepted != null)
      EULAAccepted(this, e);
  }

  public void Button1_Clicked(...)
  {
    OnEULAAccepted(new EULAEventArgs { Signature = "..." });
  }
}

public class FormA : Form
{
  public FormA()
  {
    // ...
    formB.EULAAccepted += EULAAccepted;
  }

  private void EULAAccepted(object sender, EULAEventArgs e)
  {
    this.label1.Text = String.Format("Thank you {0} for accepting the EULA.",
                                              e.Signature);
  }
}

回答by user88637

I'm not sure I understood your question but i'll try to give an answer. You can give a public property to your Form class that wrap your Label.Text Property. Then you can use this property from any other place in the code , including other classes.

我不确定我是否理解您的问题,但我会尽力给出答案。您可以为包装 Label.Text 属性的 Form 类提供一个公共属性。然后您可以在代码中的任何其他地方使用这个属性,包括其他类。

Did that answer your question? If not this is probably because I didn't understand your question , please edit and be more specific.

那回答了你的问题吗?如果不是,这可能是因为我不明白您的问题,请编辑并更具体。

回答by heartlandcoder

Is the set{} that is updating the variable and then the form running in the same thread as the form? If so then you can do what John Saunders was saying but if you need to update the Text property of the label on the form from a different thread then you will need to call do something like this from the set method:

正在更新变量的 set{} 是否与表单在同一线程中运行?如果是这样,那么您可以执行 John Saunders 所说的操作,但是如果您需要从不同的线程更新表单上标签的 Text 属性,那么您将需要从 set 方法调用执行以下操作:

theForm.TheLabel.Invoke(theForm.DelegateThatUpdatesLabel("New Text!"));