Solved

Trackbar Snap

Posted on 2004-10-19
8
1,232 Views
Last Modified: 2011-09-20
Can someone tell me how to make a trackbar snap into the position the mouse click took place.  I would like the trackbar to snap to the position I click on the trackbar with my mouse.  This is on a trackbar without tick marks.

Thanks,
Kendal
0
Comment
Question by:gvector1
  • 4
  • 4
8 Comments
 
LVL 8

Accepted Solution

by:
AaronReams earned 500 total points
ID: 12353100
This is basically one way of going about it.  It might need a little tweak near the ends of the trackbar, but you get the idea...  try increasing the maximum ticks for better resolution.

private void trackBar1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
     int numberTicks = (trackBar1.Maximum - trackBar1.Minimum)/(trackBar1.TickFrequency);
     float percent = ((float)e.X )/(float)trackBar1.Size.Width;
     int newValue = (int) ((float) numberTicks * (float)percent);
     trackBar1.Value = newValue;            
}

cheers,
Aaron
0
 
LVL 8

Expert Comment

by:AaronReams
ID: 12353132
i should also mention that the tweak at the ends i mentioned is due to the fact that the actual trackbar doesn't stretch from end-to-end of the trackbar size.  you'll notice that the trackbar has about 10 pixels of padding on each end.  it shouldn't be too hard to compensate by adding that to the above equation.

let me know if you have any questions.

good luck,
aaron
0
 

Author Comment

by:gvector1
ID: 12362129
I have looked at the trackbar and considered the tweak that you mentioned.  It looks as though the further you move from the center the more off the values are.  It is not just the ends.  The only way I can think of correcting this problem is doing some heavy calculations based on one half of the trackbar, the 10 pixels on the end, and the position of the pointer.  I am thinking along the lines of having to take half of the trackbar width, divide it by 10, and based on which section of 10 the pointer is located, adding the correct number(1-10) to the variable holding the pointers x position.  Does that sound right or am I making it harder than it is??????

Thanks,
Kendal
0
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
LVL 8

Expert Comment

by:AaronReams
ID: 12362484
That would probably work but I think that is making it a bit more complicated than necessary.    I don't  know the exact amount of pixels at each end because I think it is proportional to the length of the toolbar.  However, you can modify the percentage calculation as such to get a closer approximation:

float percent = ((float)e.X - 10)/(float)(trackBar1.Size.Width-20);

this way you're decreasing the total width by 2x and offsetting the mouse cursor by 1x.  that should do the trick.

cheers,
aaron
0
 

Author Comment

by:gvector1
ID: 12362976
Excellent.  I hate that you had to give me the calculations, but I never would have thought of that formula.

Thanks again,
Kendal
0
 
LVL 8

Expert Comment

by:AaronReams
ID: 12363125
No worries mate.   It wasn't exactly obvious how to accomplish this.   You'd think there would be a function you could call to move the trackbar slider to the current mouse position.  

-Aaron

0
 

Author Comment

by:gvector1
ID: 12363156
Exactly.  Thanks again for the assistance.

Kendal
0
 

Author Comment

by:gvector1
ID: 12444562
I'm sorry. I forgot to accept your answer as the solution.  Anyway, thanks again.
0

Featured Post

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question