Track Bar

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,

I am trying to create a control that is similar in functionality to the
TrackBar control. I cannot figure out which event to use for sliding the
image that serves as a marker over the line. I have tried the MouseMove and
MouseDown without success.

Can anyone point me in the right direction?

Thanks in advanced

Ben
 
MouseDown, MouseMove, MouseUp

Pseudo code:

OnMouseDown()
If Mouse Over ThumbTab AndAlso Left Mouse Button Pressed
EnableThumbDrag = True

OnMouseMove()
If EnableThumbDrag = True
Set ThumbTab Location and Value

OnMouseUp()
EnableThumbDrag = False
 
Hi Mickm

I tried using the 3 events below, but I still get the same behaviour as
before. I have copied the code the I have:

private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (this.EnableObjectMove == true && e.Button ==
MouseButtons.Left)
{
this.pictureBox1.Location = new Point(e.X, 3);

}
}

private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
this.EnableObjectMove = true;

}

private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
this.EnableObjectMove = false;
}

Thanks for your help!

Ben
 
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.
 
[ICR],

Your code did the trick! I am now getting the results I was after.

Thank you very much!

Ben
 
Np. Hopefully I explained it well enough/it was obvious enough that you
understand whats going on.
 
Back
Top