TextBoxBase.Clear Methode
Definitie
Belangrijk
Bepaalde informatie heeft betrekking op een voorlopige productversie die aanzienlijk kan worden gewijzigd voordat deze wordt uitgebracht. Microsoft biedt geen enkele expliciete of impliciete garanties met betrekking tot de informatie die hier wordt verstrekt.
Hiermee wist u alle tekst uit het besturingselement tekstvak.
public:
void Clear();
public void Clear();
member this.Clear : unit -> unit
Public Sub Clear ()
Voorbeelden
In het volgende codevoorbeeld wordt een afgeleide klasse gebruikt TextBoxom een gebeurtenis-handler voor de TextChanged gebeurtenis te maken. De code in de gebeurtenis-handler beperkt gegevens tot getallen. Nadat tekst in het besturingselement is ingevoerd, bepaalt de code of de ingevoerde tekst een getal is. Als de tekst geen getal is, wist de code de tekst van het besturingselement en wordt er een MessageBox weergegeven om de gebruiker te waarschuwen dat alleen getallen worden geaccepteerd. Het voorbeeld vereist dat een Boolean variabele met de naam flag en een TextBox aangeroepen besturingselement textBox1 buiten deze methode worden gedefinieerd. In dit voorbeeld ziet u hoe u een vlagvariabele gebruikt om een trapsgewijze gebeurtenis in de TextChanged gebeurtenis te voorkomen.
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
Opmerkingen
U kunt deze methode gebruiken om de inhoud van het besturingselement te wissen in plaats van de Text eigenschap een lege tekenreeks toe te wijzen.