NGWS SDK Documentation  

This is preliminary documentation and subject to change.
To comment on this topic, please send us email at ngwssdk@microsoft.com. Thanks!

TextBoxBase.Clear

Clears all text from the text box control.

[Visual Basic]
Public Sub Clear()
[C#]
public void Clear();
[C++]
public: void Clear();
[JScript]
public function Clear();

Example [Visual Basic]

The following example uses TextBox, a derived class, to create an event handler for the control's OnTextChanged 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 assumes that a Boolean variable called Flag and a TextBox control called Text1 are defined outside of this method. This example demonstrates how to use a flag to avoid a cascading event in the System.WinForms.TextBoxBase.TextChanged event.

[Visual Basic]

Private Sub MyTextChangedHandler(ByVal sender As System.Object, ByVal 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(Edit1.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.
           Edit1.Clear
       End If
       ' Reset the flag so other TextChanged events are processed correctly.
       Flag = False
   End If        
End Sub

See Also

TextBoxBase Class | TextBoxBase Members | System.WinForms Namespace | Cut | Copy | Paste | CanUndo | ClearUndo