views:

599

answers:

1

What's the easiest way to modify a Winforms track bar so that when a user clicks on it the track bar jumps to the location of the mouse?

By default the track bar moves to half way between it's current location and where the mouse was clicked.

Update: I'm trying to do this so that I can use the track bar on a touch-screen. With a touch-screen dragging is really hard, hence the requirement for the track bar to jump straight to where the user taps.

+2  A: 

I've never done it, but I would start by looking at the location of the mouse click. I could then figure out how far across the current extent of the trackbar the click was located and set the tracker's location to that spot.

I'd start by looking into overriding the control and manipulating the Mouse-related event functions (OnMouseDown(), e.g.). Note that in some cases it may be important to call the base class' implementation of an overridden function, be sure to consider it on a case-by-case basis. (I just ran afoul of this, so it's fresh in my mind.)

Note that you should only change the behavior of standard controls in this manner if you have a very compelling reason. They're standard because they behave in a predictable way across many hundreds/thousands of programs. Making something behave differently for just your program can violate the Principle of Lease Astonishment. The only trackbar I can think of that I expect to behave the way you describe is for some sort of media player.

Edit: Based on your comment, yeah, I think you've got a compelling reason. :)

Greg D
Yeah I understand this, the only problem is that my software runs on a touch screen and the default trackbar behaviour really doesn't work on a touch screen. Basically anything involving dragging it almost impossible, so the best solution I could think of was making the slider jump directly to where the user clicked.
Matt Warren
Ah, good thought. Now I know of two scenarios. Thanks for the clarification. :)
Greg D