-1

I've got a problem with subscribing from a form to an event in an user control.

MainForm-Code:

public partial class mainForm : Form
{
    public mainForm()
    {
        InitializeComponent();
        UserControl menuView = new mnlib.mnlibControl();
        newWindow(menuView);
    }

    public void newWindow(UserControl control)
    {
        this.mainPanel.Controls.Clear();
        this.mainPanel.Controls.Add(control);
    }

    mnlibControl.OnLearnClick += new EventHandler(ButtonClick); //Error in this line

    protected void ButtonClick(object sender, EventArgs e)
    {
         //handling..
    }
}

UserControl-Code:

public partial class mnlibControl : UserControl
{
    public mnlibControl()
    {
        InitializeComponent();
    }

    private void btn_beenden_Click(object sender, EventArgs e)
    {
        Application.Exit();
    }

    public event EventHandler LearnClick;
    private void btn_lernen_Click(object sender, EventArgs e)
    {
        if (this.LearnClick != null)
            this.LearnClick(this, e);
    }
}

Now, visual studio marks the "mnlibControl.OnLearnClick ..." line as wrong. "mnlibControl" would not be found, maybe a missing using directive etc. . All this code and both forms are located in the same project file. I tried around and googled like hell but just can't find a solution for my problem.

In the UserControl form there is a button - when it's clicket it shall trigger the newWindow method in the mainForm and open up another window.

My source for this solution of my problem is: How do I make an Event in the Usercontrol and Have it Handeled in the Main Form?

Community
  • 1
  • 1
dnc
  • 600
  • 1
  • 5
  • 13

3 Answers3

2

There is no OnLearnClick in your component. You need to subscribe to LearnClick. You also need to subscribe in function block. You also should use concrete type (mnlib.mnlibControl), not UserControl:

public mainForm()
{
    InitializeComponent();
    mnlib.mnlibControl menuView = new mnlib.mnlibControl();
    menuView.LearnClick += new EventHandler(ButtonClick);
    newWindow(menuView);
}
dotnetom
  • 24,551
  • 9
  • 51
  • 54
  • With exactly this code it works until the "LearnClick" in line 5. It says there would be no definition for LearnClick. – dnc Jan 10 '15 at 15:43
  • @dncrft You need to use `mnlib.mnlibControl` instead of `UserControl`. See the updated answer for details – dotnetom Jan 10 '15 at 15:51
  • Finally, this works! Thank you! No idea why I used UserControl instead of mnlib.mnlibControl .. kind of stupid ;) – dnc Jan 10 '15 at 15:52
2

Your code mnlibControl.OnLearnClick += new EventHandler(ButtonClick); must be within any of functional block (i.e. method, property, ...).

Hamlet Hakobyan
  • 32,965
  • 6
  • 52
  • 68
  • Also in this case, when I put the code in the mainForm brackets it says there would be no definition for OnLearnClick in mnlib.mnlibControl. If i change "OnLearnClick" to "LearnClick" it says, I need an object reference for "mnlib.mnlibControl.LearnClick" – dnc Jan 10 '15 at 15:50
1

You have to place this line inside an actual method:

mnlibControl.LearnClick += new EventHandler(ButtonClick);

Like this:

public mainForm()
{
    InitializeComponent();
    UserControl menuView = new mnlib.mnlibControl();
    newWindow(menuView);
    mnlibControl.OnLearnClick += new EventHandler(ButtonClick);
}
dario
  • 5,149
  • 12
  • 28
  • 32
  • This way it tells me, mnlibControl would not be in the actual context :/ – dnc Jan 10 '15 at 15:31
  • @dncrft `mnlibControl` is a class name. You have to use the instance. I mean, what's the ID of your user control in the form?? – dario Jan 10 '15 at 15:32