?
Solved

Trackbar Snap

Posted on 2004-10-19
8
Medium Priority
?
1,263 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
8 Comments
 
LVL 8

Accepted Solution

by:
AaronReams earned 2000 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
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…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses

801 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