views:

273

answers:

3

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.

+1  A: 

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;
    }
}
Charlie
A: 

You could add a parameter to your constructor that is a TextBox and save the reference in the class as a private variable and call that in each method.

Jeff Cuscutis
+1  A: 

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.

Doug L.