views:

128

answers:

3

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#.

+3  A: 

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
}
Xander
I don't want to grey out the trackbar because in my opinion greyed out components are inactive ones... which isn't my case, my trackbar is always active, sometimes just user can't modify its value, because it's modified programatically... Using `ValueChanged` event is good idea, even though the slider sometimes "flashes" when I try to move it and it immediately returns back to its correct position - that's why I wanted to somehow prevent user from moving the slider at all... But thanks a lot anyway!
Ondra C.
So I guess there´s no other way than `ValueChanged` event, so I'm accepting your answer... Thanks again...
Ondra C.
I wonder if enabling double buffering will help to reduce the flickering? Or Alternatively create your own custom control via GDI+ that will not accept user input unless specifically enabled?
Xander
A: 

If you wanted to be ambitious (or overzealous), you could create a Transparent Panel control to overlay the TrackBar thereby preventing the user from interacting with it, i.e.

   public class TransparentPanel : System.Windows.Forms.Panel
   {
      private const int WS_EX_TRANSPARENT = 0x00000020;

      protected override CreateParams CreateParams
      {
         get
         {
            CreateParams transparentParams = base.CreateParams;
            transparentParams.ExStyle |= WS_EX_TRANSPARENT;
            return transparentParams;
         }
      }

      protected override void OnPaintBackground(System.Windows.Forms.PaintEventArgs e)
      {
         // Do Nothing
      }
   }

Add this control to your form and then position it over the TrackBar, i.e.

transparentPanel.Location = trackBar1.Location;
transparentPanel.Size = trackBar1.Size;

Then you can hide it when you want to allow user interaction.

AaronY
A: 

As Xander pointed out, having a trackbar that looks perfectly normal but can't actually be used is a good way to drive your users crazy!

If you're worried about the trackbar looking "inactive", you might try another way of displaying the data it represents, like a label. That way when you disable the trackbar, you're only indicating that the editing of the value is unavailable.

Phillip Cohen
Thanks for the answer, but I really more wanted to know if (and how) it´s possible to do it... I use it probably in a different way than you think, but I don´t know any other component that would do better... I´ve edited my question and explained it there...
Ondra C.