Export (0) Print
Expand All

UpDownBase.Text Property

Gets or sets the text displayed in the spin box (also known as an up-down control).

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

[LocalizableAttribute(true)] 
public override string Text { get; set; }
/** @property */
public String get_Text ()

/** @property */
public void set_Text (String value)

public override function get Text () : String

public override function set Text (value : String)

Not applicable.

Property Value

The string value displayed in the spin box.

The UpdateEditText method is called if the Text property is set while the UserEdit property is set to true. The ValidateEditText method is called if the Text property is set while the UserEdit property is set to false.

The following code example uses the derived class NumericUpDown. This code requires that a NumericUpDown control and a Button have been created on a form and the System.Drawing namespace has been added as a reference. On the Click event for the button, the point size of text in the NumericUpDown control increases. This prompts the control to adjust its PreferredHeight property so all the text is visible in the control. After the user enters a new value and leaves the NumericUpDown control, the text is converted to a numeric value from a string value and validated to be between the Minimum and Maximum values. If the value is not valid, a MessageBox is displayed with the error, and the Select method will select the text so the user can enter a new value.

private void numericUpDown1_Leave(Object sender,
                                  EventArgs e)
{
   /* If the entered value is greater than Minimum or Maximum,
      select the text and open a message box. */
   if((System.Convert.ToInt32(numericUpDown1.Text) > numericUpDown1.Maximum) ||
      (System.Convert.ToInt32(numericUpDown1.Text) < numericUpDown1.Minimum))
   {
      MessageBox.Show("The value entered was not between the Minimum and" +
         "Maximum allowable values." + "\n" + "Please re-enter.");
      numericUpDown1.Focus();
      numericUpDown1.Select(0, numericUpDown1.Text.Length);
   }
}
   
private void button1_Click(Object sender,
                           EventArgs e)
{
   int varPrefHeight1;
   
   /* Capture the PreferredHeight before and after the Font
      is changed, and display the results in a message box. */
   varPrefHeight1 = numericUpDown1.PreferredHeight;
   numericUpDown1.Font = new System.Drawing.Font("Microsoft Sans Serif",
      12F, System.Drawing.FontStyle.Bold);
   MessageBox.Show("Before Font Change: " + varPrefHeight1.ToString() +
      "\n" + "After Font Change: " + numericUpDown1.PreferredHeight.ToString());
}


private void numericUpDown1_Leave(Object sender, EventArgs e)
{
    /* If the entered value is greater than Minimum or Maximum,
       select the text and open a message box.
     */
    if (System.Convert.ToInt32(numericUpDown1.get_Text()) > 
            System.Convert.ToInt32(numericUpDown1.get_Maximum())
            || System.Convert.ToInt32(numericUpDown1.get_Text()) <
            System.Convert.ToInt32(numericUpDown1.get_Minimum())) {
        MessageBox.Show(("The value entered was not between the Minimum and"
            + "Maximum allowable values." + "\n" + "Please re-enter."));
        numericUpDown1.Focus();
        numericUpDown1.Select(0, numericUpDown1.get_Text().length());
    }
} //NumericUpDown1_Leave

private void button1_Click(Object sender, EventArgs e)
{
    int varPrefHeight1;

    /* Capture the PreferredHeight before and after the Font
       is changed, and display the results in a message box.
     */
    varPrefHeight1 = numericUpDown1.get_PreferredHeight();
    numericUpDown1.set_Font(
         new System.Drawing.Font("Microsoft Sans Serif", 12,
         System.Drawing.FontStyle.Bold));
    MessageBox.Show(("Before Font Change: " 
        + System.Convert.ToString(varPrefHeight1)
        + "\n" + "After Font Change: " 
        + System.Convert.ToString(
            numericUpDown1.get_PreferredHeight())));
} //button1_Click

Windows 98, Windows Server 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 Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft