TextBoxBase.Clear Metódus
Definíció
Fontos
Egyes információk olyan, kiadás előtti termékekre vonatkoznak, amelyek a kiadásig még jelentősen módosulhatnak. A Microsoft nem vállal kifejezett vagy törvényi garanciát az itt megjelenő információért.
Törli az összes szöveget a szövegdoboz vezérlőelemből.
public:
void Clear();
public void Clear();
member this.Clear : unit -> unit
Public Sub Clear ()
Példák
Az alábbi példakód TextBoxegy származtatott osztály használatával hoz létre eseménykezelőt az TextChanged eseményhez. Az eseménykezelőben lévő kód számokra korlátozza az adatokat. Miután beírta a szöveget a vezérlőbe, a kód meghatározza, hogy a beírt szöveg szám-e. Ha a szöveg nem szám, a kód törli a szöveget a vezérlőből, és megjelenik egy MessageBox , amely figyelmezteti a felhasználót, hogy csak számok fogadhatók el. A példa megköveteli, hogy a metóduson kívül definiáljon egy Boolean elnevezett flag változót és egy TextBox vezérlőelemet textBox1 . Ez a példa bemutatja, hogyan használható jelzőváltozó az esemény kaszkádolt eseményeinek TextChanged elkerülésére.
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
Megjegyzések
Ezzel a módszerrel törölheti a vezérlőelem tartalmát ahelyett, hogy üres sztringet rendel a Text tulajdonsághoz.