如何:使用 Windows 窗体 NumericUpDown 控件设置和返回数值

Windows 窗体 NumericUpDown 控件的数值由其 Value 属性确定。 可以像编写任何其他属性一样为控件的值编写条件测试。 设置 Value 属性后,可以通过编写代码直接对其进行调整以对其执行操作,也可以调用 UpButtonDownButton 方法。

设置数值

  1. 在代码或“属性”窗口中为 Value 属性赋值。

    NumericUpDown1.Value = 55  
    
    numericUpDown1.Value = 55;  
    
    numericUpDown1->Value = 55;  
    

    - 或者 -

  2. 调用 UpButtonDownButton 方法,以增加或减少 Increment 属性中指定的量。

    NumericUpDown1.UpButton()  
    
    numericUpDown1.UpButton();  
    
    numericUpDown1->UpButton();  
    

返回数值

  • 访问代码中的 Value 属性。

    If NumericUpDown1.Value >= 65 Then  
       MessageBox.Show("Age is: " & NumericUpDown1.Value.ToString)  
    Else  
       MessageBox.Show("The customer is ineligible for a senior citizen discount.")  
    End If  
    
    if(numericUpDown1.Value >= 65)  
    {  
       MessageBox.Show("Age is: " + numericUpDown1.Value.ToString());  
    }  
    else  
    {  
       MessageBox.Show("The customer is ineligible for a senior citizen discount.");  
    }  
    
    if(numericUpDown1->Value >= 65)  
    {  
       MessageBox::Show(String::Concat("Age is: ",  
          numericUpDown1->Value.ToString()));  
    }  
    else  
    {  
       MessageBox::Show  
          ("The customer is ineligible for a senior citizen discount.");  
    }  
    

另请参阅