Share via


UpDownBase 类

实现数字显示框(也称为 up-down 控件)所需的基本功能。

**命名空间:**System.Windows.Forms
**程序集:**System.Windows.Forms(在 system.windows.forms.dll 中)

语法

声明
<ClassInterfaceAttribute(ClassInterfaceType.AutoDispatch)> _
<ComVisibleAttribute(True)> _
Public MustInherit Class UpDownBase
    Inherits ContainerControl
用法
Dim instance As UpDownBase
[ClassInterfaceAttribute(ClassInterfaceType.AutoDispatch)] 
[ComVisibleAttribute(true)] 
public abstract class UpDownBase : ContainerControl
[ClassInterfaceAttribute(ClassInterfaceType::AutoDispatch)] 
[ComVisibleAttribute(true)] 
public ref class UpDownBase abstract : public 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

备注

数字显示框由一个文本框和一个通常称为数值调节钮控件的小垂直滚动条组成。UpDownBase 类链接这两个控件,并允许用户通过单击向上或向下按钮或向文本框直接输入适当类型的值来更改文本框中的显示内容。要限制用户可选择的值列表时,可使用数字显示框,它与列表框或组合框类似。根据要显示的列表类型,使用数字显示框的优点在于可快速设置有效值范围,而不是一次一个地添加项。实现一个数字显示框需要执行的数据验证工作比文本框要少,因为在从 UpDownBase 派生类时可限制数据类型。这种情况的一个示例是 NumericUpDown 类,此类将值限制为数字类型,并使用 MinimumMaximum 属性验证数据。

若要使用户可以使用箭头键更改数字显示框的内容,请将 InterceptArrowKeys 属性设置为 true。若要将用户的选择限制在您指定的值范围内,请将 ReadOnly 属性设置为 true。若要控制数字显示框中的文本对齐方式,请设置 TextAlign 属性。若要设置与控件的文本框部分有关的向上按钮和向下按钮的对齐方式,可将 UpDownAlign 属性设置为 LeftRight

UpButtonDownButton 两种方法被重写后能够处理单击向上或向下按钮的操作。ValidateEditTextUpdateEditText 两种方法被重写后会验证值(选定的或输入的),并会更新显示在数字显示框中的文本。如果值验证失败,请使用 Select 方法选择无效的文本。这使得用户只需键入一个新值而不必手动选择或删除现有文本,便可快速地更正文本。

给继承者的说明 当从 UpDownBase 继承时,必须重写下面的成员:DownButtonUpButtonUpdateEditTextValidateEditText

示例

下面的代码示例使用派生类 NumericUpDown,并设置它从 UpDownBase 派生的一些属性。此代码要求您在窗体上创建了一个名为 numericUpDown1NumericUpDown 控件;两个分别名为 comboBox1comboBox2ComboBox 控件;和三个分别名为 checkBox1checkBox2checkBox2CheckBox 控件。将以下各项添加到 comboBox1NoneFixed3DFixedSingle。将以下各项添加到 comboBox2LeftRightCenter

该代码允许您在运行时更改属性值并了解每个值对数字显示框的外观与行为的影响。

Private Sub comboBox1_SelectedIndexChanged(sender As Object, e As EventArgs)
    ' Set the BorderStyle property.
    Select Case comboBox1.Text
        Case "Fixed3D"
            numericUpDown1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D
        Case "None"
            numericUpDown1.BorderStyle = System.Windows.Forms.BorderStyle.None
        Case "FixedSingle"
            numericUpDown1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
    End Select
End Sub    

Private Sub comboBox2_SelectedIndexChanged(sender As Object, e As EventArgs)
    ' Set the TextAlign property.    
    Select Case comboBox2.Text
        Case "Right"
            numericUpDown1.TextAlign = HorizontalAlignment.Right
        Case "Left"
            numericUpDown1.TextAlign = HorizontalAlignment.Left
        Case "Center"
            numericUpDown1.TextAlign = HorizontalAlignment.Center
    End Select
End Sub    

Private Sub checkBox1_Click(sender As Object, e As EventArgs)
    ' Evaluate and toggle the ReadOnly property.
    If numericUpDown1.ReadOnly Then
        numericUpDown1.ReadOnly = False
    Else
        numericUpDown1.ReadOnly = True
    End If
End Sub    

Private Sub checkBox2_Click(sender As Object, e As EventArgs)
    ' Evaluate and toggle the InterceptArrowKeys property.
    If numericUpDown1.InterceptArrowKeys Then
        numericUpDown1.InterceptArrowKeys = False
    Else
        numericUpDown1.InterceptArrowKeys = True
    End If
End Sub    

Private Sub checkBox3_Click(sender As Object, e As EventArgs)
    ' Evaluate and toggle the UpDownAlign property.
    If numericUpDown1.UpDownAlign = LeftRightAlignment.Left Then
        numericUpDown1.UpDownAlign = LeftRightAlignment.Right
    Else
        numericUpDown1.UpDownAlign = LeftRightAlignment.Left
    End If
End Sub
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;
     }
 }
 
void comboBox1_SelectedIndexChanged( Object^ sender, EventArgs^ e )
{
   // Set the BorderStyle property.
   if (  !String::Compare( comboBox1->Text, "Fixed3D" ) )
   {
      numericUpDown1->BorderStyle = System::Windows::Forms::BorderStyle::Fixed3D;
   }
   else
   if (  !String::Compare( comboBox1->Text, "None" ) )
   {
      numericUpDown1->BorderStyle = System::Windows::Forms::BorderStyle::None;
   }
   else
   if (  !String::Compare( comboBox1->Text, "FixedSingle" ) )
   {
      numericUpDown1->BorderStyle = System::Windows::Forms::BorderStyle::FixedSingle;
   }
}

void comboBox2_SelectedIndexChanged( Object^ sender, EventArgs^ e )
{
   // Set the TextAlign property.    
   if (  !String::Compare( comboBox2->Text, "Right" ) )
   {
      numericUpDown1->TextAlign = HorizontalAlignment::Right;
   }
   else
   if (  !String::Compare( comboBox1->Text, "Left" ) )
   {
      numericUpDown1->TextAlign = HorizontalAlignment::Left;
   }
   else
   if (  !String::Compare( comboBox1->Text, "Center" ) )
   {
      numericUpDown1->TextAlign = HorizontalAlignment::Center;
   }
}

void checkBox1_Click( Object^ sender, EventArgs^ e )
{
   // Evaluate and toggle the ReadOnly property.
   if ( numericUpDown1->ReadOnly )
   {
      numericUpDown1->ReadOnly = false;
   }
   else
   {
      numericUpDown1->ReadOnly = true;
   }
}

void checkBox2_Click( Object^ sender, EventArgs^ e )
{
   // Evaluate and toggle the InterceptArrowKeys property.
   if ( numericUpDown1->InterceptArrowKeys )
   {
      numericUpDown1->InterceptArrowKeys = false;
   }
   else
   {
      numericUpDown1->InterceptArrowKeys = true;
   }
}

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

线程安全

此类型的任何公共静态(Visual Basic 中的 Shared)成员都是线程安全的,但不保证所有实例成员都是线程安全的。

平台

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

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

.NET Compact Framework

受以下版本支持:2.0、1.0

请参见

参考

UpDownBase 成员
System.Windows.Forms 命名空间
NumericUpDown 类
DomainUpDown 类