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. .NET (Core and Framework)
  4. Form MouseLeave event [modified]

Form MouseLeave event [modified]

Scheduled Pinned Locked Moved .NET (Core and Framework)
questioncsharpwinformscomhelp
9 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.
  • T Offline
    T Offline
    TyrionTheImp
    wrote on last edited by
    #1

    Hi All, I have 2 main Form classes - ParentForm and ChildForm in the same exe. ChildForm has a bunch of numeric up-down controls on it. I create an instance of ChildForm, set TopLevel = true and do SetParent with the ParentForm as the parent handle (doing this enables the non client area of ChildForm to stay highlighted when its selected). Now the problem is I need to know when the mouse has left the ChildForm entirely. The Leave event for the ChildForm is never fired. If we listen to the MouseLeave event it gets fired if the mouse hovers over any of the numeric controls and also when the mouse goes to the parent form. Is there any way I can differentiate between these 2 cases ? Also note that we need to cover the case where some other external application (say notepad or Task Manager with always on top as true) is positioned exactly above the child form and is clicked EDIT UPDATE : Thanks to all for the replies ! Have asked the same question on the MSDN Forums - http://social.msdn.microsoft.com/Forums/en-US/winforms/thread/51e040a5-77f9-4b43-a462-1b5e3a13b90a/?prof=required[^] TIA

    modified on Monday, April 11, 2011 12:00 PM

    R R 2 Replies Last reply
    0
    • T TyrionTheImp

      Hi All, I have 2 main Form classes - ParentForm and ChildForm in the same exe. ChildForm has a bunch of numeric up-down controls on it. I create an instance of ChildForm, set TopLevel = true and do SetParent with the ParentForm as the parent handle (doing this enables the non client area of ChildForm to stay highlighted when its selected). Now the problem is I need to know when the mouse has left the ChildForm entirely. The Leave event for the ChildForm is never fired. If we listen to the MouseLeave event it gets fired if the mouse hovers over any of the numeric controls and also when the mouse goes to the parent form. Is there any way I can differentiate between these 2 cases ? Also note that we need to cover the case where some other external application (say notepad or Task Manager with always on top as true) is positioned exactly above the child form and is clicked EDIT UPDATE : Thanks to all for the replies ! Have asked the same question on the MSDN Forums - http://social.msdn.microsoft.com/Forums/en-US/winforms/thread/51e040a5-77f9-4b43-a462-1b5e3a13b90a/?prof=required[^] TIA

      modified on Monday, April 11, 2011 12:00 PM

      R Offline
      R Offline
      RobCroll
      wrote on last edited by
      #2
          private void Form1\_MouseLeave(object sender, EventArgs e)
          {
              Point point = this.PointToClient(Cursor.Position);
              
              if (!this.ClientRectangle.Contains(point))
                  MessageBox.Show("Ding");
          }
      

      Hope that helps

      "You get that on the big jobs."

      T 1 Reply Last reply
      0
      • R RobCroll
            private void Form1\_MouseLeave(object sender, EventArgs e)
            {
                Point point = this.PointToClient(Cursor.Position);
                
                if (!this.ClientRectangle.Contains(point))
                    MessageBox.Show("Ding");
            }
        

        Hope that helps

        "You get that on the big jobs."

        T Offline
        T Offline
        TyrionTheImp
        wrote on last edited by
        #3

        Hi Rob, Thanks for the code snippet - it works perfectly when the mouse leaves the ChildForm and moves into the ParentForm ! Howeverm for the second casse i mentioned - "some other external application (say Task Manager with always on top as true) is positioned exactly above the child form and is clicked" , the Mouse Leave event on the ChildForm is not triggered. Is there some other event i need to be listening to for this ?

        L R 2 Replies Last reply
        0
        • T TyrionTheImp

          Hi Rob, Thanks for the code snippet - it works perfectly when the mouse leaves the ChildForm and moves into the ParentForm ! Howeverm for the second casse i mentioned - "some other external application (say Task Manager with always on top as true) is positioned exactly above the child form and is clicked" , the Mouse Leave event on the ChildForm is not triggered. Is there some other event i need to be listening to for this ?

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

          I think it is hard to solve. And then your mouse could walk into a dropped-down menu or context menu too. :)

          Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

          Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

          T 1 Reply Last reply
          0
          • T TyrionTheImp

            Hi Rob, Thanks for the code snippet - it works perfectly when the mouse leaves the ChildForm and moves into the ParentForm ! Howeverm for the second casse i mentioned - "some other external application (say Task Manager with always on top as true) is positioned exactly above the child form and is clicked" , the Mouse Leave event on the ChildForm is not triggered. Is there some other event i need to be listening to for this ?

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

            Hi TIA You could start playing around with the form's Deactivate event but it probably wont completely resolve your issue. Also I'm sure you, or someone else has a good argument to implement this but it is unusual behaviour and may confuse the user. Regards Rob

            "You get that on the big jobs."

            T 1 Reply Last reply
            0
            • T TyrionTheImp

              Hi All, I have 2 main Form classes - ParentForm and ChildForm in the same exe. ChildForm has a bunch of numeric up-down controls on it. I create an instance of ChildForm, set TopLevel = true and do SetParent with the ParentForm as the parent handle (doing this enables the non client area of ChildForm to stay highlighted when its selected). Now the problem is I need to know when the mouse has left the ChildForm entirely. The Leave event for the ChildForm is never fired. If we listen to the MouseLeave event it gets fired if the mouse hovers over any of the numeric controls and also when the mouse goes to the parent form. Is there any way I can differentiate between these 2 cases ? Also note that we need to cover the case where some other external application (say notepad or Task Manager with always on top as true) is positioned exactly above the child form and is clicked EDIT UPDATE : Thanks to all for the replies ! Have asked the same question on the MSDN Forums - http://social.msdn.microsoft.com/Forums/en-US/winforms/thread/51e040a5-77f9-4b43-a462-1b5e3a13b90a/?prof=required[^] TIA

              modified on Monday, April 11, 2011 12:00 PM

              R Offline
              R Offline
              Ravi Bhavnani
              wrote on last edited by
              #6

              TyrionTheImp wrote:

              Also note that we need to cover the case where some other external application
              (say notepad or Task Manager with always on top as true) is positioned exactly above the child form and is clicked

              Do you instead want to handle the Control.LostFocus[^] event? /ravi

              My new year resolution: 2048 x 1536 Home | Articles | My .NET bits | Freeware ravib(at)ravib(dot)com

              T 1 Reply Last reply
              0
              • L Luc Pattyn

                I think it is hard to solve. And then your mouse could walk into a dropped-down menu or context menu too. :)

                Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

                Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

                T Offline
                T Offline
                TyrionTheImp
                wrote on last edited by
                #7

                Hey Luc, Regarding the context menus and tool strip/main menus we can listen to their Activated events, so i don`t think that should be an issue :) The tricky part is to differentiate between my applications client area and other windows that may be place directly above it. In that case, MosueLeave gets fired and the even if the user hovers over the other window, the Cursor is still within the client area boundaries ! One would expect WinForms to provide some notifications for such a basic event but i haven`t (yet) found anything helpful in the Win32 API layer as well.

                1 Reply Last reply
                0
                • R RobCroll

                  Hi TIA You could start playing around with the form's Deactivate event but it probably wont completely resolve your issue. Also I'm sure you, or someone else has a good argument to implement this but it is unusual behaviour and may confuse the user. Regards Rob

                  "You get that on the big jobs."

                  T Offline
                  T Offline
                  TyrionTheImp
                  wrote on last edited by
                  #8

                  Hi Rob, Yes Deactivated would work when the user actually activates another window thus deactivates my window. However, the behavior i`ve described does not really seem unusual - for example if the user is working with my application - and the mouse enters/leaves the child windows and the parent window , then certain parts of the UI are highlighted accordingly - thus reducing user confusion :)

                  1 Reply Last reply
                  0
                  • R Ravi Bhavnani

                    TyrionTheImp wrote:

                    Also note that we need to cover the case where some other external application
                    (say notepad or Task Manager with always on top as true) is positioned exactly above the child form and is clicked

                    Do you instead want to handle the Control.LostFocus[^] event? /ravi

                    My new year resolution: 2048 x 1536 Home | Articles | My .NET bits | Freeware ravib(at)ravib(dot)com

                    T Offline
                    T Offline
                    TyrionTheImp
                    wrote on last edited by
                    #9

                    Hey Ravi, Thanks for the reply - will try it in combination with the code suggested by Rob earlier.

                    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