Win Coder
Win Coder

Reputation: 6756

event handler to detect mouse drag on picture box(winforms,c#)

I am making simple paint application in which a line would be drawn whenever someone holds down the mouse button and drags(exactly like in windows paint).

However i am having a hard time finding the suitable event handler for this. MouseDown is simply not working and MouseClick is only jotting down dots whenever i press a mouse down.

Need help in this matter.

Thanks.

Upvotes: 5

Views: 7906

Answers (1)

e_ne
e_ne

Reputation: 8469

Handle MouseDown and set a boolean variable to true. Handle MouseMove and, if the variable is set to true and the mouse's movement is above your desired treshold, operate. Handle MouseUp and set that variable to false.

Example:

bool _mousePressed;
private void OnMouseDown(object sender, MouseEventArgs e)
{
    _mousePressed = true;
}

private void OnMouseMove(object sender, MouseEventArgs e)
{
    if (_mousePressed)
    {
        //Operate
    }
}

private void OnMouseUp(object sender, MouseEventArgs e)
{
    _mousePressed = false;
}

Upvotes: 15

Related Questions