Blazi
Blazi

Reputation: 1011

How to raise an event inside a UserControl to handle it outside, in another project?

I come after this question. in my CustomControl I want to handle all inner controls' MouseClick events for example, then raise an event for the customControl, So I can handle it in every project that I use the control.

private void UserControl1_Load(object sender, EventArgs e)
{
    foreach (var control in Controls)
    {
        ((Control)control).MouseClick += UserControl1_Click;

    }
}

private void UserControl1_Click(object sender, MouseEventArgs e)
{
    // here I want to raise a MouseClick event for UserControl1?
}

How can I solve this problem? ( I think it maybe is like when we catch an exception then throw it again to somewhere else catch it?)

Upvotes: 1

Views: 1262

Answers (2)

user1912419
user1912419

Reputation: 496

Try this:

private void UserControl1_Click(object sender, MouseEventArgs e)
{
    // Raise MouseClick event for this control
    base.OnMouseClick(e);
}

Upvotes: 1

SysDragon
SysDragon

Reputation: 9888

Simply:

Public Event MouseClick(ByVal sender As Object, ByVal e As MouseEventArgs)

Private Sub UserControl1_Click (object sender, MouseEventArgs e)
    RaiseEvent MouseClick(sender, e)
End Sub

And then outside handle the MouseClick event. (I put the code in Vb.Net because you tagged it in your question, but is easily to convert it).

Upvotes: 1

Related Questions