Solved

Trackbar Snap

Posted on 2004-10-19
8
1,196 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

Author Comment

by:gvector1
Comment Utility
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
Comment Utility
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
Comment Utility
Exactly.  Thanks again for the assistance.

Kendal
0
 

Author Comment

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

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
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.

772 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