UpDownBase.Select(Int32, Int32) Método

Definição

Seleciona um intervalo de texto na caixa de rotação (também conhecido como controle para cima/para baixo) que especifica a posição inicial e o número de caracteres a selecionar.

public:
 void Select(int start, int length);
public void Select (int start, int length);
override this.Select : int * int -> unit
Public Sub Select (start As Integer, length As Integer)

Parâmetros

start
Int32

A posição do primeiro caractere a ser selecionado.

length
Int32

O número total de caracteres a ser selecionado.

Exemplos

O exemplo de código a seguir usa a classe NumericUpDownderivada . Esse código requer que um NumericUpDown controle e um Button tenham sido criados em um formulário e o System.Drawing namespace tenha sido adicionado como referência. No evento do Click botão, o tamanho do ponto do texto no NumericUpDown controle aumenta. Isso solicita que o controle ajuste sua PreferredHeight propriedade para que todo o texto fique visível no controle. Depois que o usuário insere um novo valor e deixa o NumericUpDown controle, o texto é convertido em um valor numérico de um valor de cadeia de caracteres e validado para estar entre os Minimum valores e Maximum . Se o valor não for válido, um MessageBox será exibido com o erro e o Select método selecionará o texto para que o usuário possa inserir um novo valor.

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 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 = gcnew System::Drawing::Font( "Microsoft Sans Serif",12.0,System::Drawing::FontStyle::Bold );
   MessageBox::Show( String::Format( "Before Font Change: {0}\nAfter Font Change: {1}", varPrefHeight1, numericUpDown1->PreferredHeight ) );
}
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 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

Comentários

O Select método pode ser usado quando a caixa de rotação obtém o foco ou quando a propriedade falha na Text validação de dados. Ao adicionar o código de validação para o ValidateEditText método em uma classe derivada, chame o Select método quando a validação falhar.

Aplica-se a

Confira também