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
Solved

Trackbar Snap

Posted on 2004-10-19
8
1,216 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
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.

 
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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

Suggested Solutions

Title # Comments Views Activity
Printing 1 58
Getting error in connectionstring with Excel. 30 45
Create a WebAPI using a generic return parameter 9 30
tableview is not updating 1 20
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
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.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

839 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