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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Mouse Events

Mouse Events

Scheduled Pinned Locked Moved C#
hostinghelp
7 Posts 4 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
    rich_wenger
    wrote on last edited by
    #1

    Hi, I have an app that needs to trap mouse events across a form hosting multiple controls for a keep-alive timer. Any help would be greatly appreciated.

    D 1 Reply Last reply
    0
    • R rich_wenger

      Hi, I have an app that needs to trap mouse events across a form hosting multiple controls for a keep-alive timer. Any help would be greatly appreciated.

      D Offline
      D Offline
      Dan Neely
      wrote on last edited by
      #2

      Write handlers for some or all of the mouse events: MouseDown, MouseUp, MouseEnter, MouseHover, MouseLeave, and MouseMove events, and wire them to *every* control on the form.

      R 1 Reply Last reply
      0
      • D Dan Neely

        Write handlers for some or all of the mouse events: MouseDown, MouseUp, MouseEnter, MouseHover, MouseLeave, and MouseMove events, and wire them to *every* control on the form.

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

        Hi, and thanks for responding. Lets say I write mouse events for Form1; how do I wire them to 'every' control on the form? this.MouseMove +=new MouseEventHandler(Form1_MouseMove); private void Form1_MouseMove(object sender, MouseEventArgs e) { //Do Something here }

        L 1 Reply Last reply
        0
        • R rich_wenger

          Hi, and thanks for responding. Lets say I write mouse events for Form1; how do I wire them to 'every' control on the form? this.MouseMove +=new MouseEventHandler(Form1_MouseMove); private void Form1_MouseMove(object sender, MouseEventArgs e) { //Do Something here }

          L Offline
          L Offline
          Luis Alonso Ramos
          wrote on last edited by
          #4

          rich_wenger wrote:

          how do I wire them to 'every' control on the form?

          this.MouseMove +=new MouseEventHandler(Form1_MouseMove);
          btnOK.MouseMove += new MouseEventHandler(Form1_MouseMove);
          txtName.MouseMove += new MouseEventHandler(Form1_MouseMove);
          chkRemember.MouseMove += new MouseEventHandler(Form1_MouseMove);
          

          and so on. Or you could also do this:

          foreach(Control ctl in this.Controls)
              ctl.MouseMove += new MouseEventHandler(Form1_MouseMove);
          

          If you have controls inside other controls (panels or group boxes for example), you'll need to do that recursively. If you always use the same handler, the same routine will be called for events raised by any controls. I hope this helps. -- LuisR


          Luis Alonso Ramos Intelectix - Chihuahua, Mexico Not much here: My CP Blog!

          The amount of sleep the average person needs is five more minutes. -- Vikram A Punathambekar, Aug. 11, 2005

          G 1 Reply Last reply
          0
          • L Luis Alonso Ramos

            rich_wenger wrote:

            how do I wire them to 'every' control on the form?

            this.MouseMove +=new MouseEventHandler(Form1_MouseMove);
            btnOK.MouseMove += new MouseEventHandler(Form1_MouseMove);
            txtName.MouseMove += new MouseEventHandler(Form1_MouseMove);
            chkRemember.MouseMove += new MouseEventHandler(Form1_MouseMove);
            

            and so on. Or you could also do this:

            foreach(Control ctl in this.Controls)
                ctl.MouseMove += new MouseEventHandler(Form1_MouseMove);
            

            If you have controls inside other controls (panels or group boxes for example), you'll need to do that recursively. If you always use the same handler, the same routine will be called for events raised by any controls. I hope this helps. -- LuisR


            Luis Alonso Ramos Intelectix - Chihuahua, Mexico Not much here: My CP Blog!

            The amount of sleep the average person needs is five more minutes. -- Vikram A Punathambekar, Aug. 11, 2005

            G Offline
            G Offline
            Gulfraz Khan
            wrote on last edited by
            #5

            And you can use the following sample code to add mousemove event handler to every control on your form (as Luis pointed out). //you may call the function in the Form_Load event as AttachEventHandler(this); void AttachEventHandler(Control ParentControl) { foreach (Control ctrl in ParentControl.Controls) { if (ctrl.HasChild == true) { AttachEventHandler(ctrl); // recursive call, add handler to child controls also } else { ctrl.MouseMove += new MouseEventHandler(Form1_MouseMove); } } }

            R 2 Replies Last reply
            0
            • G Gulfraz Khan

              And you can use the following sample code to add mousemove event handler to every control on your form (as Luis pointed out). //you may call the function in the Form_Load event as AttachEventHandler(this); void AttachEventHandler(Control ParentControl) { foreach (Control ctrl in ParentControl.Controls) { if (ctrl.HasChild == true) { AttachEventHandler(ctrl); // recursive call, add handler to child controls also } else { ctrl.MouseMove += new MouseEventHandler(Form1_MouseMove); } } }

              R Offline
              R Offline
              rich_wenger
              wrote on last edited by
              #6

              Thanks, I'll give it a try after lunch.

              1 Reply Last reply
              0
              • G Gulfraz Khan

                And you can use the following sample code to add mousemove event handler to every control on your form (as Luis pointed out). //you may call the function in the Form_Load event as AttachEventHandler(this); void AttachEventHandler(Control ParentControl) { foreach (Control ctrl in ParentControl.Controls) { if (ctrl.HasChild == true) { AttachEventHandler(ctrl); // recursive call, add handler to child controls also } else { ctrl.MouseMove += new MouseEventHandler(Form1_MouseMove); } } }

                R Offline
                R Offline
                rich_wenger
                wrote on last edited by
                #7

                Your solution works except for the following statment:

                Gulfraz Khan wrote:

                if (ctrl.HasChild == true)

                It should actually read: if (ctrl.HasChildren == true) Thanks again.

                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