Complex user control not responding to click events
-
Hi all, I'm wondering whether any of you people can help me out with this problem that I have with a user control that I have been trying to get working. The first part of the user control is a flat circular button ( called SampleButton) that toggles colour when clicked. This control has an OnClick method as follows protected override void OnClick(EventArgs e) { base.OnClick(e); (code to change colour and call invalidate....) } Then, from this I am creating a matrix of these buttons in another user control that inherits from SampleButton (which in turn inherits from UserControl), e.g. a 3 x 3 matrix, like so: O O O O O O O O O I do this using a couple of for loops (the number of SampleButtons has to be dynamic), a bit like: for (int i = 0; i <= 2; i++) { for (int j = 0; j <= 2; j++) { SampleButton samBtn = new SampleButton(); samBtn.Location = new Point(i, j); this.Controls.Add(samBtn); } } } In this control I have an OnClick method: protected override void OnClick(EventArgs e) { base.OnClick(e); MessageBox.Show("The Sample Button Array has been clicked"); } The problem is that although when I click on the array of sample buttons, their colour changes (so the first OnClick method is running ok), the messagebox never appears (the method is never entered). Why does this second OnClick method never get called? Thanks, Rob
-
Hi all, I'm wondering whether any of you people can help me out with this problem that I have with a user control that I have been trying to get working. The first part of the user control is a flat circular button ( called SampleButton) that toggles colour when clicked. This control has an OnClick method as follows protected override void OnClick(EventArgs e) { base.OnClick(e); (code to change colour and call invalidate....) } Then, from this I am creating a matrix of these buttons in another user control that inherits from SampleButton (which in turn inherits from UserControl), e.g. a 3 x 3 matrix, like so: O O O O O O O O O I do this using a couple of for loops (the number of SampleButtons has to be dynamic), a bit like: for (int i = 0; i <= 2; i++) { for (int j = 0; j <= 2; j++) { SampleButton samBtn = new SampleButton(); samBtn.Location = new Point(i, j); this.Controls.Add(samBtn); } } } In this control I have an OnClick method: protected override void OnClick(EventArgs e) { base.OnClick(e); MessageBox.Show("The Sample Button Array has been clicked"); } The problem is that although when I click on the array of sample buttons, their colour changes (so the first OnClick method is running ok), the messagebox never appears (the method is never entered). Why does this second OnClick method never get called? Thanks, Rob
When you click a SampleButton its OnClick fires, and it causes its base.OnClick to fire since that is what you requested; but its base is Button, not your 3x3UserControl. If your 3x3UserControl wants events for its buttons, it should include the line samBtn.Click+=new EventHandler(...); where it creates the buttons. :)
Luc Pattyn
-
When you click a SampleButton its OnClick fires, and it causes its base.OnClick to fire since that is what you requested; but its base is Button, not your 3x3UserControl. If your 3x3UserControl wants events for its buttons, it should include the line samBtn.Click+=new EventHandler(...); where it creates the buttons. :)
Luc Pattyn
Hi, Thanks for your reply Luc. I added the code to add event handlers for each button and tested the event within the User Control designer and all seems fine there. However, when I add a Button Array to a windows form, and create a Click event for the whole array: private void buttonArray1_Click(object sender, EventArgs e) { MessageBox.Show("Button array has been clicked"); } (and this.buttonArray1.Click += new System.EventHandler(this.buttonArray1_Click) in the initialiseComponent method) This event does not trigger. This was one reason why I added the OnClick method to the ButtonArray control as I thought that by doing this it would allow clicks on the ButtonArray to be handled within my form (my logic is probably completely wrong here) Thanks again, Rob
-
Hi, Thanks for your reply Luc. I added the code to add event handlers for each button and tested the event within the User Control designer and all seems fine there. However, when I add a Button Array to a windows form, and create a Click event for the whole array: private void buttonArray1_Click(object sender, EventArgs e) { MessageBox.Show("Button array has been clicked"); } (and this.buttonArray1.Click += new System.EventHandler(this.buttonArray1_Click) in the initialiseComponent method) This event does not trigger. This was one reason why I added the OnClick method to the ButtonArray control as I thought that by doing this it would allow clicks on the ButtonArray to be handled within my form (my logic is probably completely wrong here) Thanks again, Rob
Hi, as far as I understand, when you click the mouse, Windows will fire one event, towards the control that "most deserves" it. If two controls dont overlap, things are obvious; if one control is inside another in the normal way, the smallest one would get the event. (Behavior will be influenced by Z-order, by Visible=false, Enabled=false and possibly many others). From there on it is up to your code who else may get an event: - your base class if you call base.OnClick - and/or anything else that has told your control it is interested (as you now do with the 3x3UserControl signing up to each of the samBtn. SO if you have an object buttonArray1 that is interested in the clicks of samBtn, you must apply a samBtn.Click+=new ..., and not a buttonArray1.Click+=new ...) Hope this clarifies things. :)
Luc Pattyn
-
Hi, as far as I understand, when you click the mouse, Windows will fire one event, towards the control that "most deserves" it. If two controls dont overlap, things are obvious; if one control is inside another in the normal way, the smallest one would get the event. (Behavior will be influenced by Z-order, by Visible=false, Enabled=false and possibly many others). From there on it is up to your code who else may get an event: - your base class if you call base.OnClick - and/or anything else that has told your control it is interested (as you now do with the 3x3UserControl signing up to each of the samBtn. SO if you have an object buttonArray1 that is interested in the clicks of samBtn, you must apply a samBtn.Click+=new ..., and not a buttonArray1.Click+=new ...) Hope this clarifies things. :)
Luc Pattyn
Hi, I also had the samBtn.Click+=....in the loop attaching a common handler each button ...to no avail. However your comments got me thinking about exactly what was happening (in terms of only 1 event and overlapping controls) and I think I realise why its not working. The problem is that I have a usercontrol that is *full* of buttons. When I added the buttonArray1.Click+= part to the user control and hopeing to catch this, it was not firing because that would *only* fire if I clicked on an *empty* part of the userControl and not a button. If I click on a button, then that SampleButton "most deserves it" and deals with it correctly (it changes colour). As there is no part of the user control that is empty, its never going to work. Maybe what I need to do is raise a new event from the change colour event and catch that in my main form. Thanks again, Rob