TextBoxBase.Clear 方法

从文本框控件中清除所有文本。

**命名空间:**System.Windows.Forms
**程序集:**System.Windows.Forms(在 system.windows.forms.dll 中)

语法

声明
Public Sub Clear
用法
Dim instance As TextBoxBase

instance.Clear
public void Clear ()
public:
void Clear ()
public void Clear ()
public function Clear ()

备注

可以使用此方法清除控件的内容,而不是给 Text 属性分配一个空字符串。

示例

下面的代码示例使用派生类 TextBoxTextChanged 事件创建一个事件处理程序。该事件处理程序中的代码将数据限制在数字范围内。在控件中输入文本后,代码将确定所输入的文本是否是数字。如果该文本不是数字,则代码将从控件中清除该文本,并显示一个 MessageBox,警告用户只接受数字。此示例要求在此方法的外部定义了一个名为 flagBoolean 变量和一个名为 textBox1TextBox 控件。此示例说明如何使用 flag 变量避免 TextChanged 事件中出现级联事件。

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
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:
   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 boolean flag;
private void MyTextChangedHandler(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.get_Text());
        }
        catch (System.Exception exp) {
            // 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;
    }
} //MyTextChangedHandler

平台

Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

请参见

参考

TextBoxBase 类
TextBoxBase 成员
System.Windows.Forms 命名空间
Cut
Copy
Paste
CanUndo
ClearUndo