Re: Track Bar
- From: "[ICR]" <andrew@xxxxxxxxxxxxxxxx>
- Date: 4 Jun 2006 11:51:26 -0700
The problem is most likely because e.X is relative to the control, but
you are setting the position relative to the form. You are also not
taking into account where abouts in the image the cursor was clicked.
Here is the code I use:
private void tracker_MouseDown(object sender, MouseEventArgs e)
{
// Very small optimisation. Check here, so we don't have to check
EVERY time the mouse is moved
if (e.Button == MouseButtons.Left)
{
ShouldDrag = true;
// Store the intitial cursor position. This allows us to offset
the tracker so that it doesn't jump to the left of the cursor.
initialCursorPosition = e.X;
}
}
private void tracker_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
ShouldDrag = false;
}
private void tracker_MouseMove(object sender, MouseEventArgs e)
{
if (ShouldDrag)
{
// Move it based on the offset, not the absolute value. Also,
offset it so the tracker doesn't jump.
tracker.Left += (e.X - initialCursorPosition);
}
}
If you don't understand the "offset" things, remove that part of the
code to see. I couldn't think how best to explain it.
Hope that helps.
.
- References:
- Re: Track Bar
- From: Mick Doherty
- Re: Track Bar
- From: Ben
- Re: Track Bar
- Prev by Date: Re: Track Bar
- Next by Date: Re: Tooltip, .ShowDialog(), and .Dispose() on .NET 2.0 generate a NullReferenceException
- Previous by thread: Re: Track Bar
- Next by thread: Re: Track Bar
- Index(es):