简体   繁体   中英

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{}. 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.

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". 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.

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:

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.

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. 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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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