TextBoxBase.Clear 方法

定义

从文本框控件中清除所有文本。Clears all text from the text box control.

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

示例

下面的代码示例使用派生类 TextBoxTextChanged 事件创建事件处理程序。The following code example uses TextBox, a derived class, to create an event handler for the TextChanged event. 事件处理程序中的代码将数据限制为数字。The code within the event handler restricts data to numbers. 在控件中输入文本后,代码会确定输入的文本是否为数字。After text has been entered in the control, the code determines if the text entered is a number. 如果文本不是数字,则代码将清除控件中的文本,并显示一个 MessageBox 以提醒用户仅接受数字。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. 该示例要求名为 flagBoolean 变量和一个名为 textBox1TextBox 控件在此方法的外部定义。The example requires that a Boolean variable named flag and a TextBox control called textBox1 are defined outside of this method. 此示例演示如何使用标志变量来避免 TextChanged 事件中出现级联事件。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

注解

您可以使用此方法清除控件的内容,而不是将 Text 属性分配为空字符串。You can use this method to clear the contents of the control instead of assigning the Text property an empty string.

适用于

另请参阅