Export (0) Print
Expand All

UpDownBase.Select Method (Int32, Int32)

Selects a range of text in the spin box (also known as an up-down control) specifying the starting position and number of characters to select.

Namespace:  System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)

public void Select(
	int start,
	int length
)

Parameters

start
Type: System.Int32

The position of the first character to be selected.

length
Type: System.Int32

The total number of characters to be selected.

The Select method can be used when the spin box gets focus, or when the Text property fails data validation. When adding the validation code for the ValidateEditText method in a derived class, call the Select method when validation fails.

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 that 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());
}

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

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

Show:
© 2014 Microsoft