TextBoxBase.Clear TextBoxBase.Clear TextBoxBase.Clear TextBoxBase.Clear Method

Definición

Borra todo el texto del control de cuadro de texto.Clears all text from the text box control.

public:
 void Clear();
public void Clear ();
member this.Clear : unit -> unit
Public Sub Clear ()

Ejemplos

El siguiente ejemplo de código usa TextBox, una clase derivada, para crear un controlador de eventos para el TextChanged eventos.The following code example uses TextBox, a derived class, to create an event handler for the TextChanged event. El código del controlador de eventos restringe los datos a los números.The code within the event handler restricts data to numbers. Una vez que ha escrito texto en el control, el código determina si el texto escrito es un número.After text has been entered in the control, the code determines if the text entered is a number. Si el texto no es un número, el código borra el texto del control y un MessageBox se muestra para avisar al usuario que se aceptan solo números.If the text is not a number, the code clears the text from the control and a MessageBox is displayed to alert the user that only numbers are accepted. El ejemplo requiere que un Boolean variable denominada flag y un TextBox control denominado textBox1 se definen fuera de este método.The example requires that a Boolean variable named flag and a TextBox control called textBox1 are defined outside of this method. Este ejemplo muestra cómo utilizar una variable de indicador para evitar un evento en cascada en la TextChanged eventos.This example demonstrates how to use a flag variable to avoid a cascading event in the TextChanged event.

private:
   bool flag;

private:
   void MyTextChangedHandler( System::Object^ sender, System::EventArgs^ e )
   {
      Int64 val;
      // Check the flag to prevent code re-entry. 
      if ( flag == false )
      {
         // Set the flag to True to prevent re-entry of the code below.
         flag = true;
         // Determine if the text of the control is a number.
         try
         {
            // Attempt to convert to long
            val = System::Convert::ToInt64( textBox1->Text );
         }
         catch ( Exception^ ) 
         {
            // Display a message box and clear the contents if not a number.
            MessageBox::Show( "The text is not a valid number. Please re-enter" );
            // Clear the contents of the text box to allow re-entry.
            textBox1->Clear();
         }
         // Reset the flag so other TextChanged events are processed correctly.
         flag = false;
      }
   }
private bool flag;

private void MyTextChangedHandler(System.Object sender, System.EventArgs e)
{
    long val;    
    // Check the flag to prevent code re-entry. 
    if(flag == false)
    {
       // Set the flag to True to prevent re-entry of the code below.
       flag = true;
       // Determine if the text of the control is a number.
       try {
          // Attempt to convert to long
          val = System.Convert.ToInt64(textBox1.Text);
       }
       catch {
          // Display a message box and clear the contents if not a number.
          MessageBox.Show("The text is not a valid number. Please re-enter");
          // Clear the contents of the text box to allow re-entry.
          textBox1.Clear();
       }
       // Reset the flag so other TextChanged events are processed correctly.
       flag = false;
    }        
 }
 
Private flag As Boolean    

Private Sub MyTextChangedHandler(sender As System.Object, e As System.EventArgs)
    ' Check the flag to prevent code re-entry. 
    If flag = False Then
        ' Set the flag to True to prevent re-entry of the code below.
        flag = True
        ' Determine if the text of the control is a number.
        If IsNumeric(textBox1.Text) = False Then
            ' Display a message box and clear the contents if not a number.
            MessageBox.Show("The text is not a valid number. Please re-enter")
            ' Clear the contents of the text box to allow re-entry.
            textBox1.Clear()
        End If
        ' Reset the flag so other TextChanged events are processed correctly.
        flag = False
    End If
End Sub

Comentarios

Puede usar este método para borrar el contenido del control en lugar de asignar la Text propiedad una cadena vacía.You can use this method to clear the contents of the control instead of assigning the Text property an empty string.

Se aplica a

Consulte también: