Bi.
Bi.

Reputation: 1906

pass information from class to Winforms form

How do I pass a row information from my class to a grid in the windows form of my application? The row information changes every now and then and I need to pass this updated information to the form

Upvotes: 0

Views: 418

Answers (1)

Ed Swangren
Ed Swangren

Reputation: 124632

You can expose an event in your class that the form class can subscribe to. When that event is triggered the form can update the UI as needed. For example:

class ChildForm : Form
{
    public event EventHandler TextChanged;

    public string NewText { get { return textBox1.Text; } }

    void textBox1_TextChanged( object sender, EventArgs e )
    {
        EventHandler del = TextChanged;
        if( del != null )
        {
            del( this, e );
        }
    }
}

class MainForm : Form
{  
    void Foo( )
    {
        using( ChildForm frm = new ChildForm )
        {
            frm.TextChanged += (object sender, EventArgs e) => { label1.Text = frm.NewText; };
            frm.ShowDialog( );
        }
    }
}

You could actually just pass the TextBox.TextChanged event right no through in this example.

Upvotes: 3

Related Questions