Freigeben über


TextBoxBase.Clear Methode

Definition

Löscht den gesamten Text aus dem Textfeld-Steuerelement.

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

Beispiele

Im folgenden Codebeispiel wird TextBoxeine abgeleitete Klasse verwendet, um einen Ereignishandler für das TextChanged Ereignis zu erstellen. Der Code innerhalb des Ereignishandlers schränkt Daten auf Zahlen ein. Nachdem Text in das Steuerelement eingegeben wurde, bestimmt der Code, ob der eingegebene Text eine Zahl ist. Wenn der Text keine Zahl ist, löscht der Code den Text aus dem Steuerelement und wird MessageBox angezeigt, um den Benutzer darüber zu informieren, dass nur Zahlen akzeptiert werden. Das Beispiel erfordert, dass eine Boolean Variable namens flag und ein aufgerufenes TextBoxtextBox1 Steuerelement außerhalb dieser Methode definiert sind. In diesem Beispiel wird veranschaulicht, wie Sie eine Flagvariable verwenden, um ein kaskadierendes Ereignis im TextChanged Ereignis zu vermeiden.

private:
   bool flag;

private:
   void MyTextChangedHandler( System::Object^ sender, System::EventArgs^ e )
   {
      Int64 val;
      // Check the flag to prevent code re-entry. 
      if ( !flag )
      {
         // 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)
    {
       // 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

Hinweise

Mit dieser Methode können Sie den Inhalt des Steuerelements löschen, anstatt der Text Eigenschaft eine leere Zeichenfolge zuzuweisen.

Gilt für:

Weitere Informationen