简体   繁体   中英

How can I update a TextBox from a class using events?

I would like to update a form's textbox in winforms with data that is being processed from a class file.

Example:

Class.RunProcess();

Inside of RunProcess I would like to fire an event that will update the textbox with the text that I pass in on the win form.

I know you can do this with events so I don't have to make the textbox public or pass it into the many methods I have, I am just not quite sure how to start.

Edit : Maybe I am just not clear.

I have a windows form with a text box on it.

I have a button that runs a process.

The process is in a class file outside of the form class.

Inside this process method I would like to be able to update the text box on the main form without having to pass the TextBox as a parameter to all the methods that I will have (I currently have 6 and will be more).

I thought I could subscribe to an event that would allow me to display a message in the text box while the processes ran in my class.

It's probably time to consider:

  1. Databinding your textbox to your class
  2. Model/View/Presenter (MVP)

For #1 (Databinding), you can use the INotifyPropertyChanged interface on your class and raise the event for changed properties.

public class BusinessModel : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    private int _quantity;
    public int Quantity
    {
        get { return _quantity; }
        set 
        { 
            _quantity = value;
            this.OnPropertyChanged("Quantity");            
        }
    }

    void OnPropertyChanged(string PropertyName)
    {
        if (this.PropertyChanged != null)
        {
            this.PropertyChanged(this, new PropertyChangedEventArgs(PropertyName));
        }
    }
 }

And in your form, youcan bind the .Text property of the textBox to the object in several ways. Through the UI or in code.

Links:

Now, if you need to add to text that already exists such as in your example, you can either track the full text in the class or raise events from your class and respond in the form code. Tracking it in the class would be better - you really don't want any logic in the form at all, which brings us back to binding and/or some form of MVP/MVC.

If you're wanting Class.RunProcess to run when an event from an external class is fired then you can do it like this

public delegate void MyDelegate();

public class MyClass
{
    public event MyDelegate Myevent;

    public void DoSomething()
    {
        this.Myevent();
    }
}
public static class Class
{
    public static void RunProcess()
    {
        txtData.Text += "Message";
    }
}
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        MyClass myClass = new MyClass();
        myClass.Myevent += Class.RunProcess;
    }
}

您可以向构造函数中添加一个参数,该参数是一个TextBox,并将该类中的引用保存为私有变量,并在每个方法中调用该参数。

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