UpDownBase.Select(Int32, Int32) Metodo

Definizione

Seleziona un intervallo di testo nella casella di selezione, detta anche controllo di scorrimento, specificando la posizione iniziale e il numero di caratteri da selezionare.

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)

Parametri

start
Int32

Posizione del primo carattere da selezionare.

length
Int32

Numero totale dei caratteri da selezionare.

Esempio

Nell'esempio di codice seguente viene utilizzata la classe NumericUpDownderivata . Questo codice richiede che un NumericUpDown controllo e un Button oggetto siano stati creati in una maschera e che lo System.Drawing spazio dei nomi sia stato aggiunto come riferimento. Nell'evento Click per il pulsante aumenta la dimensione del punto del testo nel NumericUpDown controllo . In questo modo viene richiesto al controllo di regolarne PreferredHeight la proprietà in modo che tutto il testo sia visibile nel controllo . Dopo che l'utente immette un nuovo valore e lascia il NumericUpDown controllo, il testo viene convertito in un valore numerico da un valore stringa e convalidato per essere compreso tra i Minimum valori e Maximum . Se il valore non è valido, viene visualizzato un MessageBox oggetto con l'errore e il Select metodo selezionerà il testo in modo che l'utente possa immettere un nuovo valore.

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

Commenti

Il Select metodo può essere utilizzato quando la casella di selezione ottiene lo stato attivo o quando la proprietà non riesce la Text convalida dei dati. Quando si aggiunge il codice di convalida per il ValidateEditText metodo in una classe derivata, chiamare il Select metodo quando la convalida ha esito negativo.

Si applica a

Vedi anche