[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.
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.
Re: MouseMove event fires for no reason
How about ignoring the first one or two mouse moves you get after you maximize the window?
Re: MouseMove event fires for no reason
Quote:
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.