0

I am new to C# windows forms and I have question about firing an event in a user control and handle it in another form.

I have Form1, Form2 and UserControl1.

Form2 is shown using showdialog() from Form1 and the UserControl1 is added in Form2.

There is an event in UserControl1 I want to handle in Form1. I know to do this Form1 subscribes to an event in Form2 and Form2 subscribes to an event in UserControl1 and when the UserControl1 event gets fired it is handled in Form2 and then Form2 fires its event and this is handled in From1.

But I am looking for different approach. I want to fire event from UserControl1 straight to From1 and handle it in From1 without Form2 intervention.

Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
Sam
  • 281
  • 1
  • 4
  • 13
  • If your Form1 has a reference to the uc it can hook into its events (or actions..). But of course the uc will still sit in form2. – TaW Jun 07 '18 at 15:56
  • 1
    is this windows forms or ASP.NET forms? – ADyson Jun 07 '18 at 15:57
  • 1
    You could try to get the event via reflection. You could look into [this](https://stackoverflow.com/questions/23567944/getting-event-via-reflection) but i would like to quote the most usefull answer: **this is almost guaranteed to be a really terrible idea** – Pulle Jun 07 '18 at 15:59
  • ADyson: windows forms – Sam Jun 07 '18 at 16:00
  • 2
    The variables that the designer generates are normally *private*. As well they should be. Basic reason why the Form1 class can't get to the events raised by the control owned by the Form2 class. Easy to change, select the UC on Form2 and change its Modifiers property from Private to Public. Now you do get to fret a bit about what might happen when user closes the Form2 instance but Form1 still has an event handler for a control that no longer exists. Its okay. – Hans Passant Jun 07 '18 at 16:23

0 Answers0