This documentation is archived and is not being maintained.

TextBoxBase.Clear Method

Clears all text from the text box control.

Namespace: System.Windows.Forms
Assembly: System.Windows.Forms (in system.windows.forms.dll)

public void Clear ()
public void Clear ()
public function Clear ()

You can use this method to clear the contents of the control instead of assigning the Text property an empty string.

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. 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. The example requires that a Boolean variable named flag and a TextBox control called textBox1 are defined outside of this method. 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)
{
    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 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

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0
Show: