简体   繁体   English

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

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

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{}. 我所拥有的是使用get {}和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. 我知道我需要在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. 我希望这是有意义的,并提前感谢。

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;
    }
}

If I understand your question, you have a form class, "form1". 如果我理解你的问题,你就有一个表单类“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上的标签。

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! 那一切都应该开心!

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);
  }
}

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. 您可以为包含Label.Text属性的Form类提供公共属性。 Then you can use this property from any other place in the code , including other classes. 然后,您可以在代码中的任何其他位置使用此属性,包括其他类。

Did that answer your question? 这回答了你的问题吗? If not this is probably because I didn't understand your question , please edit and be more specific. 如果不是这可能是因为我不理解您的问题,请编辑并更具体。

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: 如果是这样,那么你可以做John Saunders所说的,但是如果你需要从另一个线程更新表单上标签的Text属性,那么你需要从set方法调用类似的东西:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM