Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Complex user control not responding to click events

Complex user control not responding to click events

Scheduled Pinned Locked Moved C#
helpquestiondata-structures
5 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    robalexclark
    wrote on last edited by
    #1

    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

    L 1 Reply Last reply
    0
    • R robalexclark

      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

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      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

      R 1 Reply Last reply
      0
      • L 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

        R Offline
        R Offline
        robalexclark
        wrote on last edited by
        #3

        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

        L 1 Reply Last reply
        0
        • R robalexclark

          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

          L Offline
          L Offline
          Luc Pattyn
          wrote on last edited by
          #4

          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

          R 1 Reply Last reply
          0
          • L 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

            R Offline
            R Offline
            robalexclark
            wrote on last edited by
            #5

            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

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups