• 0

[C#] User Control Events not firing


Question

Is this a built in feature of C# that I'm missing... I create a bunch of user controls, and add them to my main Form1...

inside the Creation of the user Control.. I try to add Events

			InitializeComponent();
....
			this.Click += new EventHandler(Bin_Click);
			this.MouseHover += new EventHandler(Bin_MouseHover);

And below I have the definitions for the events... But they are not getting fired when I mouseover or click the User Control... What gives?

Link to comment
https://www.neowin.net/forum/topic/618506-c-user-control-events-not-firing/
Share on other sites

5 answers to this question

Recommended Posts

  • 0

Let me clarify...

My User control has a couple different controls.. Ultimatele I want a click to be captured on anywhere inside the usercontrol...

It only works right now for areas on which dont have a user control, merely just the background.

Add the Click event to all controls... I'm assuming the answer I found is going to be the one i ge... guess there's no way to capture them all without capturing mouse events?

  • 0

Yea, as you said, I'm not at all sure you can bind the event to the User Control itself.

Have you tried putting everything in the User Control inside one Panel and binding the Click/OnMouseOver eventhandlers to the Panel instead? I haven't tried this, but _I_ would try this first.

And depending on where you actually want to do the processing of those events you might need to bubble the event up to Page (search for "bubble events asp.net").

  • 0

Override the OnMouseClick method and it should work.

protected override void OnMouseClick(MouseEventArgs e)

{

// label1 belongs to the user control

label1.Text = String.Format("{0} mouse button clicked at {1},{2}", e.Button,

e.X, e.Y);

base.OnMouseClick(e);

}

The same would apply to any event you want to utilize.

Edited by azcodemonkey
This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.