CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Jan 2007
    Posts
    491

    [RESOLVED]MouseMove event fires for no reason

    Hi.
    I'm writing an application which uses the MouseMove and Resize events.
    Here's a skeleton of my code
    Code:
            private void MainForm_Resize(object sender, EventArgs e)
            {
                //do stuff
            }
    
            private void MainForm_MouseMove(object sender, MouseEventArgs e)
            {
                if (e.Button == MouseButtons.Left)
                {
                    //do some other stuff
                }
            }
    Now, when I maximize the window by double clicking the blue bar at the top of the form, the MouseMove event fires, and the program enters the "//do some other stuff" code segment.

    It happens because once I finish double clicking, the form is maximized, and since the mouse is now inside the bounds of the form, it threats my double-click as a click on the form (even though I finished doubled-clicking BEFORE the form size was changed).

    Obviously, it's an unwanted behavior. I don't want to threat the double-click, which is supposed to maximize the window, as an actual click on the form.

    So... I tried to fix the Resize event, by writing that instead:
    Code:
            private void MainForm_Resize(object sender, EventArgs e)
            {
                this.MouseMove -= new MouseEventHandler(MainForm_MouseMove);
                //do stuff
                this.MouseMove += new MouseEventHandler(MainForm_MouseMove);
            }
    but the MouseMove event still fires.

    I even tried to call the Thread.Sleep() method inside the Resize event, but it didn't help either.

    Is there a way to fix it?

    Thanks.
    Last edited by Talikag; November 28th, 2010 at 07:40 AM.

  2. #2
    Join Date
    May 2005
    Location
    San Antonio Tx
    Posts
    44

    Re: MouseMove event fires for no reason

    well i wouldn't call this "undesired behavior" nor "fires for no reason" because as you explained there is an actual click on the form so this code will definitely fire
    Code:
    if (e.Button == MouseButtons.Left)
    {
           //do some other stuff
    }
    you are almost blatantly asking for that behavior

    i would suggest that you analyze exactly what it is your are trying to do with the MoveMouse event and see if you can use another event that does not overlap with the resize event or consider creating a custom event.

  3. #3
    Join Date
    May 2007
    Posts
    1,546

    Re: MouseMove event fires for no reason

    How about ignoring the first one or two mouse moves you get after you maximize the window?
    www.monotorrent.com For all your .NET bittorrent needs

    NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.

  4. #4
    Join Date
    Jan 2007
    Posts
    491

    Re: MouseMove event fires for no reason

    well i wouldn't call this "undesired behavior" nor "fires for no reason" because as you explained there is an actual click on the form so this code will definitely fire
    no, it isn't. I don't click the actual form, but the blue bar at the top of it.

    Anyway, it turns out if you use the MouseUp / MouseDown events, the event won't fire. So it's working fine now.

    thanks for your help.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured