Handling cursor drag

J

Jon Cosby

I need an event handler for dragging the cursor on a PictureBox. The
existing events only include handlers for dragging and dropping objects over
the controls. I'm trying to use the MouseDown and MouseUp handlers as below,
but it never gets out of the while loop.

this.PicBox.MouseDown += new MouseEventHandler(this.StartMapDrag);
this.PicBox.MouseUp += new MouseEventHandler(this.EndMapDrag);

private void StartMapDrag(object sender, MouseEventArgs e)
{
drag = true;
WhileDrag();
}

private void EndMapDrag(object sender, MouseEventArgs e)
{
drag = false;
}

private void WhileDrag()
{
while (drag)
{
// Do something here
}
}

Can someone tell me a better way to handle this event?


Jon Cosby
 
F

Frans Bouma [C# MVP]

I need an event handler for dragging the cursor on a PictureBox. The
existing events only include handlers for dragging and dropping objects
over the controls. I'm trying to use the MouseDown and MouseUp handlers
as below, but it never gets out of the while loop.

this.PicBox.MouseDown += new MouseEventHandler(this.StartMapDrag);
this.PicBox.MouseUp += new MouseEventHandler(this.EndMapDrag);

private void StartMapDrag(object sender, MouseEventArgs e)
{
drag = true;
WhileDrag();
}

private void EndMapDrag(object sender, MouseEventArgs e)
{
drag = false;
}

private void WhileDrag()
{
while (drag)
{
// Do something here
}
}

Can someone tell me a better way to handle this event?

The gui is single threaded. This means that the gui's process is
executing the WhileDrag while loop and is never doing something else. You
could opt for an Application.DoEvents() in teh while(drag) loop, as that
would handle all messages currently stored at the message pump and for
example would hten call a mouse handler routine.

Drag and drop however is not something you should babysit. Drag &
Drop starts when a user selects something and moves the mouse while
keeping the lmb down. All you then have to monitor is when teh mouse
button is released. If that's the case inside your window (f.e. in a
picture box) you have to check if a drag & drop action is going on. If so,
do a copy action for example, of the selected item(s) which were selected
when the drag started.

FB
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top