Export (0) Print
Expand All

UpDownBase.Select Method

Selects a range of text in the up-down control.

Overload List

Selects a range of text in the up-down control specifying the starting position and number of characters to select.

[Visual Basic] Overloads Public Sub Select(Integer, Integer)
[C#] public void Select(int, int);
[C++] public: void Select(int, int);
[JScript] public function Select(int, int);

Inherited from Control.

[Visual Basic] Overloads Public Sub Select()
[C#] public void Select();
[C++] public: void Select();
[JScript] public function Select();

Inherited from Control.

[Visual Basic] Overloads Protected Overridable Sub Select(Boolean, Boolean)
[C#] protected virtual void Select(bool, bool);
[C++] protected: virtual void Select(bool, bool);
[JScript] protected function Select(Boolean, Boolean);

Example

[Visual Basic, C#, C++] The following example uses the derived class, NumericUpDown. This code assumes 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 of 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 new a new value.

[Visual Basic, C#, C++] Note   This example shows how to use one of the overloaded versions of Select. For other examples that might be available, see the individual overload topics.
[Visual Basic] 
Private Sub numericUpDown1_Leave(sender As Object, e As EventArgs)
    ' 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) Or _
        (System.Convert.ToInt32(numericUpDown1.Text) < numericUpDown1.Minimum) Then
        MessageBox.Show("The value entered was not between the Minimum and " & _
            "Maximum allowable values." & Microsoft.VisualBasic.ControlChars.Cr & _
            "Please re-enter.")
        numericUpDown1.Focus()
        numericUpDown1.Select(0, numericUpDown1.Text.Length)
    End If
End Sub    

Private Sub button1_Click(sender As Object, e As EventArgs)
    Dim varPrefHeight1 As Integer
    
    ' 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() & _
        Microsoft.VisualBasic.ControlChars.Cr & "After Font Change: " & _
        numericUpDown1.PreferredHeight.ToString())
End Sub


[C#] 
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());
}


[C++] 
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(S"The value entered was not between the Minimum andMaximum allowable values.\nPlease re-enter.");
      numericUpDown1->Focus();
      numericUpDown1->Select(0, numericUpDown1->Text->Length);
   }
}
   
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(S"Microsoft Sans Serif",
      12.0, System::Drawing::FontStyle::Bold);
   MessageBox::Show(String::Format( S"Before Font Change: {0}\nAfter Font Change: {1}",
      __box(varPrefHeight1), __box(numericUpDown1->PreferredHeight)));
}

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

See Also

UpDownBase Class | UpDownBase Members | System.Windows.Forms Namespace

Show:
© 2014 Microsoft