Monday, January 09, 2006

When maximum is not a maximum (Windows Form’s Scrollbar control)

  • You want to allow to your users to set a range value in a visual manner.

Possible solutions in Windows Forms
  • Use a TrackBar control or a ScrollBar (HScrollBar or VScrollBar) control.

We wanted to mimic the UI of MS Word when you set the Fill transparency value for an AutoShape. In the Fill Effects dialog box you’ll find that most transparency controls are HScrollBar that allows you to set it in a range from 0 to 100.
So, to mimic that UI, we dragged & dropped an HScrollBar control onto a Form. We set their properties Minimum to 0 (zero) and Maximum to 100. All seems to be easy and straightforward until you run your form.
At runtime, if you scroll to the minimum you’ll get the minimum value i.e. 0 (zero). But if you scroll to the maximum you’ll get 91. How is it possible?
Well the answer is in the Maximum property help on MSDN that states:

Note The value of a scroll bar cannot reach its maximum value through user interaction at run time. The maximum value that can be reached is equal to the Maximum property value minus the LargeChange property value plus one. The maximum value can only be reached programmatically.

Ah… that makes sense :-S

Maximum (100) – LargeChange (10) + 1 = 91

Ok. If that is the logic behind ScrollBar controls, then in order you can reach a Maximum value at runtime scrolling it we’ll need to set the Maximum property to:

Maximum desired (100) + LargeChange (10) – 1 = 109

That’s it. No comments.


Post a Comment

<< Home