eugeneK
eugeneK

Reputation: 11116

How to inform parent control when UserControl perform method call?

I have MainForm on which i've loaded UserControl. This UserControl has few textboxes and save button. Once i click save, information from textboxes is saved to file. I want to inform MainForm that information is updated to it can reload.

How can i do that ?

Upvotes: 2

Views: 1386

Answers (2)

ken2k
ken2k

Reputation: 48985

Use events.

Declare an event in your UserControl, for instance:

public event EventHandler SaveClicked;

then on save click, raise the event:

if (this.SaveClicked != null)
{
    this.SaveClicked(this, EventArgs.Empty);
}

and finally attach a handler in the main form to your event:

...
YourUserControl ctrl = new YourUserControl();
ctrl.SaveClicked += (sender, e) =>
{
    // Put logic of your main form here
};

Upvotes: 5

Yanshof
Yanshof

Reputation: 9926

You can use event - the userControl will send event to the parent - and the parent will register as event listener.

Upvotes: 1

Related Questions