2

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 ?

eugeneK
  • 10,750
  • 19
  • 66
  • 101

2 Answers2

5

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
};
ken2k
  • 48,145
  • 10
  • 116
  • 176
  • Actually this is not a tricky part. See this discussion: http://stackoverflow.com/questions/786383/c-sharp-events-and-thread-safety – ken2k Dec 26 '11 at 10:25
  • @ken2k that is highly interesting - I'll delete my original comment, it now seems inaccurate to me (read this in Essential C# 3.0, Michaelis - interesting how the experts can be wrong). – Adam Dec 26 '11 at 10:36
1

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

Yanshof
  • 9,659
  • 21
  • 95
  • 195