TextBoxBase.Clear Metodo

Definizione

Cancella tutto il testo dal controllo casella di testo.

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

Esempio

Nell'esempio di codice seguente viene TextBoxutilizzato , una classe derivata, per creare un gestore eventi per l'evento TextChanged . Il codice all'interno del gestore eventi limita i dati ai numeri. Dopo che il testo è stato immesso nel controllo , il codice determina se il testo immesso è un numero. Se il testo non è un numero, il codice cancella il testo dal controllo e viene visualizzato un oggetto MessageBox per avvisare l'utente che vengono accettati solo numeri. L'esempio richiede che una Boolean variabile denominata flag e un TextBox controllo chiamato textBox1 siano definiti all'esterno di questo metodo. In questo esempio viene illustrato come usare una variabile flag per evitare un evento a catena nell'evento TextChanged .

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

Commenti

È possibile utilizzare questo metodo per cancellare il contenuto del controllo anziché assegnare alla Text proprietà una stringa vuota.

Si applica a

Vedi anche