Solved

Trackbar Snap

Posted on 2004-10-19
8
1,205 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Windows Service to Receive TCP Packets 4 117
Create XML 5 46
IO.FileSystemWatcher dies when used in Powershell 1 44
Export import database 4 39
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

930 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now