Search code examples
c#winformstrackbar

System.Windows.Forms.Trackbar - how to prevent user from dragging the slider?


Is there some way to (temporarily) prevent user from changing the value of TrackBar by dragging the slider? Only way I found is to set Enabled property to false but it also greys out the trackbar.

Edit: Since I´m getting more answers about why shouldn't I do it rather than how it's possible to do it, I decided to explain why I would like to do it.

I'm not using it to allow user adjust value of some application property, but to display and control progress of some action. Imagine for example your favourite media player - it probably contains some control (I'd call it trackbar but English is not my native language so it's maybe wrong) that displays what part of movie it's currently playing, but also allows you to control it - move back or forward in time and watch the different part.

I use the trackbar in exactly this way - I don't know any other component that would be better (Progressbar won't allow me changing the "position"). It's working fine, the only thing I'd like to do is not to allow user to "use the trackbar unless the movie is paused".

For this exact reason I've used trackbar component many times in Delphi 6, but when it was disabled it didn't grey out and in my opinion it worked fine. That's why I asked here if it's possible to achieve the same effect in C#.


Solution

  • The best way would be to set the Enabled property to false, as you rightly pointed out it greys out the track bar too.

    Greying out the controls is a windows standard for saying "You cannot use this control at the moment", just imagine you were presented with some controls that was not grayed out where disabled, it would be a very hit and miss affair trying them all to see which is enabled and which isn't.

    If you really want to prevent changes without using the Enabled property one possible alternative is to use the ValueChanged event of the TrackBar

    private void trackBar1_ValueChanged(object sender, EventArgs e)
    {
      // Code here to restore the original value
    }