Visual Basic

  1. Home
  2. Computing & Technology
  3. Visual Basic
The UpDown Components - New Cool Tools in .NET
More reasons why you can do more with .NET!

UpDown Tools

A mantra that you often see in connection with VB .NET is Faster, Easier, Better. If you're just learning or moving up from VB 6, the "Faster and Easier" part might be called into question some of the time. But then you run into cool tools like the UpDown components and you can see where things really could be a lot faster and easier.

These tools don't do anything that VB 6 couldn't do with a few keystrokes ... well ... a few hundred keystrokes, maybe. And third party software vendors have long offered similar tools for VB 6. But these are built into the base product in VB .NET (with the implied reliability and future-proofing) and they really do work well. Let's put them through their paces.

The two controls under consideration are DomainUpDown and NumericUpDown.

A check of recent VB .NET books shows that there is remarkably little documentation available for these controls. For example, the normally comprehensive and encyclopedic thousand pager, Visual Basic .NET for Experienced Programmers by the Deitel consulting group doesn't have a word about it. The college textbook level Introduction to Visual Basic Using .NET covers the controls but misses some significant points.

Although these controls have a lot of similarity on the surface, when you dig into their methods and properties, you find they're quite different. The main thing that is the same is their user interface: a textbox combined with a spin button combined on one control. The alternative to using one of these is the drop-down list box (or ComboBox) control. This does have the advantage of giving users better random access to a collection of values, so it your data doesn't have some natural sequence to it, you might want to consider the ComboBox instead.

To illustrate the DomainUpDown and NumericUpDown controls, I decided to dip back into a favorite topic first raised in the About Visual Basic article, Computer Number Systems. In spite of a career in programming, I still have difficulty remembering the key multipliers used in different number systems. If you have a number 500 in Hex, is that 5 times 1024? 512? I can't remember. So I built a little application to tell me. Here's the application (with the answer to my question displayed).

UpDown Application To build the application, the first step is to drop both controls (and associated list and text box controls) onto a form. The Wrap property is set to True on the DomainUpDown control (named NumberSystem in this program) so you can continuously scroll in either direction to get to the number system you want.

Here's the code that was used to implement our little application:

Private Sub RadixVal_Load( _
    ByVal sender As Object, _
    ByVal e As System.EventArgs) _
    Handles MyBase.Load
    Dim NumberSystemTypes() As String = _
        {"Septuagintal", "Hexadecimal", _
        "Octal", "Binary"}
    NumberSystem.Items.AddRange(NumberSystemTypes)
    'Initialize the control to the first member of 
    'the Collection
    NumberSystem.SelectedIndex = _
        NumberSystemTypes.GetLength(0) - 1
    ValueOfPosition.Text = 1
End Sub

Private Sub NumberSystem_SelectedItemChanged( _
    ByVal sender As System.Object, _
    ByVal e As System.EventArgs) _
    Handles NumberSystem.SelectedItemChanged
    If NumberSystem.SelectedIndex <> -1 Then
        ValueOfPosition.Text = CalcVal()
    End If
End Sub

Private Sub NumberPlace_ValueChanged( _
    ByVal sender As System.Object, _
    ByVal e As System.EventArgs) _
    Handles NumberPlace.ValueChanged
    If NumberSystem.SelectedIndex <> -1 Then
        ValueOfPosition.Text = CalcVal()
    End If
End Sub

Function BaseVal( _
    ByVal NumberSystemIndex As Integer) _
    As Integer
    Select Case NumberSystem.SelectedIndex()
        Case 0
            Return 17
        Case 1
            Return 16
        Case 2
            Return 8
        Case 3
            Return 2
        Case Else
            MsgBox("This should never happen")
    End Select
End Function

Function CalcVal()
    CalcVal = _
    CStr(BaseVal(NumberSystem.SelectedIndex) _
    ^ (NumberPlace.Value - 1))
End Function

It seemed to me that the top spin button didn't go "up" in the sequence like it should. To fix that, it was necessary to order the collection of string values backward and initialize the display at the "top" rather than "bottom" of the collection.

NumericUpDown, on the other hand, doesn't implement a Wrap method and is more properly a "range" control because it's initialized with a Minimum and Maximum properties. Since we're dealing with exponential numbers here, the values can get out of hand very quickly. Since NumericUpDown limits the numbers to only those that are in the range, it neatly avoids any requirement to add a range check for an input value in a text box, for example.

Note the initialization of the DomainUpDown control as well. The default value of SelectedIndex is -1. This means that the user has to click the down spin button a couple of times before anything (and, in particular, the Wrap property) works. Initializing the control eliminates this ... uhhhhh ... unusual behavior. (It couldn't be a bug!)

The code can be downloaded HERE.

Explore Visual Basic

By Category

About.com Special Features

Visual Basic

  1. Home
  2. Computing & Technology
  3. Visual Basic

©2009 About.com, a part of The New York Times Company.

All rights reserved.