Export (0) Print
Expand All
0 out of 1 rated this helpful - Rate this topic

UpDownBase Class

Implements the basic functionality required by a spin box (also known as an up-down control).

Namespace: System.Windows.Forms
Assembly: System.Windows.Forms (in system.windows.forms.dll)

[ClassInterfaceAttribute(ClassInterfaceType.AutoDispatch)] 
[ComVisibleAttribute(true)] 
public abstract class UpDownBase : ContainerControl
/** @attribute ClassInterfaceAttribute(ClassInterfaceType.AutoDispatch) */ 
/** @attribute ComVisibleAttribute(true) */ 
public abstract class UpDownBase extends ContainerControl
ClassInterfaceAttribute(ClassInterfaceType.AutoDispatch) 
ComVisibleAttribute(true) 
public abstract class UpDownBase extends ContainerControl

A spin box consists of a text box and a small vertical scroll bar, commonly referred to as a spin button control. The UpDownBase class links the two controls and allows the user to change the display in the text box by clicking the up or down buttons or by entering the appropriate type of value directly into the text box. Use the spin box in cases where you want to limit the list of values a user can select, similar to a list box or combo box. Depending upon the type of list you want to display, the advantage to using a spin box is that it allows you to quickly set a range of valid values, rather than adding items one at a time. Implementing a spin box requires less data validation than a text box, as you can limit the data type when you derive a class from UpDownBase. An example of this is the NumericUpDown class, which limits the values to the numeric type and uses a Minimum and Maximum property to validate the data.

To allow the user to use the arrow keys to change the contents of a spin box, set the InterceptArrowKeys property to true. To restrict the user to values you specify, set the ReadOnly property to true. To control the alignment of text in the spin box, set the TextAlign property. To set the alignment of the up and down buttons in relation to the text box portion of the control, set the UpDownAlign property to either Left or Right.

The UpButton and DownButton methods, when overridden, handle the clicking of the up or down buttons. When overridden, the ValidateEditText and UpdateEditText methods validate the value (either selected or entered) and update the text displayed in the spin box. If the value fails validation, use the Select method to select the text that is not valid. This allows the user to quickly correct the text by simply typing in a new value without having to manually select or delete the existing text.

Notes to Inheritors When you inherit from UpDownBase, you must override the following members: DownButton, UpButton, UpdateEditText, and ValidateEditText.

The following code example uses the derived class NumericUpDown and sets some of its properties derived from UpDownBase. This code requires that you have a NumericUpDown control named numericUpDown1, two ComboBox controls named comboBox1 and comboBox2, and three CheckBox controls named checkBox1, checkBox2, and checkBox2 created on a form. Add the following items to comboBox1: None, Fixed3D, and FixedSingle. Add the following items to comboBox2: Left, Right, and Center.

The code allows you to change the property values at run time and see how each affects the appearance and behavior of the spin box.

private void comboBox1_SelectedIndexChanged(Object sender, 
                                             EventArgs e)
 {
      // Set the BorderStyle property.
     switch(comboBox1.Text)
     {
         case "Fixed3D":
             numericUpDown1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
             break;
         case "None":
             numericUpDown1.BorderStyle = System.Windows.Forms.BorderStyle.None;
             break;
         case "FixedSingle":
             numericUpDown1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
             break;
     }
 }
 
 private void comboBox2_SelectedIndexChanged(Object sender, 
                                             EventArgs e)
 {
      // Set the TextAlign property.    
     switch (comboBox2.Text)
     {
         case "Right":
             numericUpDown1.TextAlign = HorizontalAlignment.Right;
             break;
         case "Left":
             numericUpDown1.TextAlign = HorizontalAlignment.Left;
             break;
         case "Center":
             numericUpDown1.TextAlign = HorizontalAlignment.Center;
             break;
     }
 }
 
 private void checkBox1_Click(Object sender, 
                              EventArgs e)
 {
      // Evaluate and toggle the ReadOnly property.
     if (numericUpDown1.ReadOnly)
     {
         numericUpDown1.ReadOnly = false;
     }
     else
     {
         numericUpDown1.ReadOnly = true;
     }
 }
 
 private void checkBox2_Click(Object sender, 
                              EventArgs e)
 {
      // Evaluate and toggle the InterceptArrowKeys property.
     if (numericUpDown1.InterceptArrowKeys)
     {  
         numericUpDown1.InterceptArrowKeys = false;
     }
     else
     {
         numericUpDown1.InterceptArrowKeys = true;
     }
 }
 
 private void checkBox3_Click(Object sender, 
                              EventArgs e)
 {
      // Evaluate and toggle the UpDownAlign property.
     if (numericUpDown1.UpDownAlign == LeftRightAlignment.Left)
     {
         numericUpDown1.UpDownAlign = LeftRightAlignment.Right;
     }
     else
     {
         numericUpDown1.UpDownAlign = LeftRightAlignment.Left;
     }
 }
 

    private void comboBox1_SelectedIndexChanged(Object sender, EventArgs e)
    {
        // Set the BorderStyle property.
        if (comboBox1.get_Text().Equals("Fixed3D")) {
            numericUpDown1.set_BorderStyle(BorderStyle.Fixed3D);
        }
        else {
            if (comboBox1.get_Text().Equals("None")) {
                numericUpDown1.set_BorderStyle(BorderStyle.None);
            }
            else {
                if (comboBox1.get_Text().Equals("FixedSingle")) {
                    numericUpDown1.set_BorderStyle(BorderStyle.FixedSingle);
                }
            }
        }        
    }//comboBox1_SelectedIndexChanged
    
    private void comboBox2_SelectedIndexChanged(Object sender, EventArgs e)
    {
        // Set the TextAlign property.    
        if (comboBox2.get_Text().Equals("Right")) {
            numericUpDown1.set_TextAlign(HorizontalAlignment.Right);
        }
        else {
            if (comboBox2.get_Text().Equals("Left")) {
                numericUpDown1.set_TextAlign(HorizontalAlignment.Left);
            }
            else {
                if (comboBox2.get_Text().Equals("Center")) {
                    numericUpDown1.set_TextAlign(HorizontalAlignment.Center);
                }
            }
        }
    } //comboBox2_SelectedIndexChanged

    private void checkBox1_Click(Object sender, EventArgs e)
    {
        // Evaluate and toggle the ReadOnly property.
        if (numericUpDown1.get_ReadOnly()) {
            numericUpDown1.set_ReadOnly(false);
        }
        else {
            numericUpDown1.set_ReadOnly(true);
        }
    } //checkBox1_Click

    private void checkBox2_Click(Object sender, EventArgs e)
    {
        // Evaluate and toggle the InterceptArrowKeys property.
        if (numericUpDown1.get_InterceptArrowKeys()) {
            numericUpDown1.set_InterceptArrowKeys(false);
        }
        else {
            numericUpDown1.set_InterceptArrowKeys(true);
        }
    } //checkBox2_Click

    private void checkBox3_Click(Object sender, EventArgs e)
    {
        // Evaluate and toggle the UpDownAlign property.
        if (numericUpDown1.get_UpDownAlign().Equals(LeftRightAlignment.Left)) {
            numericUpDown1.set_UpDownAlign(LeftRightAlignment.Right);
        }
        else {
            numericUpDown1.set_UpDownAlign(LeftRightAlignment.Left );
        }
    } //checkBox3_Click
} //Form1 

System.Object
   System.MarshalByRefObject
     System.ComponentModel.Component
       System.Windows.Forms.Control
         System.Windows.Forms.ScrollableControl
           System.Windows.Forms.ContainerControl
            System.Windows.Forms.UpDownBase
               System.Windows.Forms.DomainUpDown
               System.Windows.Forms.NumericUpDown
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.