TextBoxBase.Clear Método
Definição
Importante
Algumas informações dizem respeito a um produto pré-lançado que pode ser substancialmente modificado antes de ser lançado. A Microsoft não faz garantias, de forma expressa ou implícita, em relação à informação aqui apresentada.
Apaga todo o texto do controlo da caixa de texto.
public:
void Clear();
public void Clear();
member this.Clear : unit -> unit
Public Sub Clear ()
Exemplos
O exemplo de código seguinte utiliza TextBox, uma classe derivada, para criar um gestor de eventos para o TextChanged evento. O código dentro do gestor de eventos restringe os dados a números. Depois de o texto ter sido introduzido no controlo, o código determina se o texto introduzido é um número. Se o texto não for um número, o código apaga o texto do controlo e um MessageBox é exibido para alertar o utilizador de que apenas os números são aceites. O exemplo exige que uma Boolean variável nomeada flag e um TextBox controlo chamado textBox1 sejam definidas fora deste método. Este exemplo demonstra como usar uma variável flag para evitar um evento em cascata no TextChanged evento.
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
Observações
Pode usar este método para limpar o conteúdo do controlo em vez de atribuir à Text propriedade uma cadeia vazia.