如何:使用 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.");
    }
    

另请参阅