zackiv31 Posted February 8, 2008 Share Posted February 8, 2008 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 More sharing options...
0 zackiv31 Posted February 8, 2008 Author Share Posted February 8, 2008 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? Link to comment https://www.neowin.net/forum/topic/618506-c-user-control-events-not-firing/#findComment-589193515 Share on other sites More sharing options...
0 ramesees Posted February 8, 2008 Share Posted February 8, 2008 Where are you defining the following methods: 1) Bin_Click 2) Bin_MouseHover The above code doesnt go in the user control but on the form where the user control is added Link to comment https://www.neowin.net/forum/topic/618506-c-user-control-events-not-firing/#findComment-589193791 Share on other sites More sharing options...
0 BertilDator Posted February 8, 2008 Share Posted February 8, 2008 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"). Link to comment https://www.neowin.net/forum/topic/618506-c-user-control-events-not-firing/#findComment-589193815 Share on other sites More sharing options...
0 azcodemonkey Posted February 8, 2008 Share Posted February 8, 2008 (edited) 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 February 8, 2008 by azcodemonkey Link to comment https://www.neowin.net/forum/topic/618506-c-user-control-events-not-firing/#findComment-589194355 Share on other sites More sharing options...
0 zackiv31 Posted February 8, 2008 Author Share Posted February 8, 2008 The events work inside the user control itself... but becuase i have ~5 controls in it, I had to add the Click event to each of them. But that does work. Link to comment https://www.neowin.net/forum/topic/618506-c-user-control-events-not-firing/#findComment-589194708 Share on other sites More sharing options...
Question
zackiv31
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