When I put a button on a form in C#, Visual Studio 2005, and have an action triggered by a button event, such as MouseHover or MouseDown, then the event triggers a single call to the function which defines the action despite the fact that I may continue to hover or keep the left button down. In this case I am trying to move a graphical object by rotating or translating it. I don't want to continue to click the mouse in order to get a repeated call to the transforming function, just keep the mouse hovering or hold the button down. What maintains the action until I cease my own action?
A:
Set a flag on MouseEnter and keep doing the action while the flag remains true. Set the flag to false on MouseLeave.
Charlie
2009-07-15 22:05:24
I tried the above with the following code: private void MouseEnter_ZoomIn(object sender, EventArgs e) { more = true; // while (more == true) if (more == true) { c1Chart3D1.ChartArea.View.ViewportScale *= ZoomMultiple; } } // MOUSEENTER_ZOOMIN() //------------------------------------- private void MouseLeave_Stop(object sender, EventArgs e) { more = false; }I was hoping that the MouseLeave event would kill the while-loop, but it didn't.
2009-07-16 15:47:10
A:
In your case you need to use a combination of the events MouseDown, MouseMove and MouseUp.
Here a small simplified example to start:
private void OnMouseDown(object sender, EventArgs e)
{
//hit test to check if the mouse pointer is on a graphical object
_myHitObject = the_selected_object
}
private void OnMouseMove(object sender, EventArgs e)
{
if(_myHitObject != null)
//do your action relative to the mouse movements.
}
private void OnMouseUp(object sender, EventArgs e)
{
_myHitObject = null;
}
Francis B.
2009-07-15 22:10:20
A:
The solution is to use DoEvents() which allows for the MouseLeave event to be noted and the class variable "more" to be changed:
private void MouseEnter_ZoomIn(object sender, EventArgs e)
{
more = true;
while (more == true)
{
c1Chart3D1.ChartArea.View.ViewportScale *= ZoomMultiple;
Application.DoEvents();
}
} // MOUSEENTER_ZOOMIN()
//-------------------------------------
private void MouseLeave_Stop(object sender, EventArgs e)
{
more = false;
}