TextBoxBase.Clear 메서드

정의

TextBox 컨트롤의 모든 텍스트를 삭제합니다.

public:
 void Clear();
public void Clear ();
member this.Clear : unit -> unit
Public Sub Clear ()

예제

다음 코드 예제에서는 파생 클래스인 를 사용하여 TextBox이벤트에 대한 이벤트 처리기를 만듭니다 TextChanged . 이벤트 처리기 내의 코드는 데이터를 숫자로 제한합니다. 컨트롤에 텍스트를 입력한 후 코드는 입력한 텍스트가 숫자인지 여부를 결정합니다. 텍스트가 숫자가 아닌 경우 코드는 컨트롤에서 텍스트를 지우고 MessageBox 는 숫자만 수락됨을 사용자에게 알리기 위해 표시됩니다. 이 예제에서는 Boolean 라는 flag 변수와 라는 textBox1 컨트롤이 TextBox 이 메서드 외부에서 정의되어야 합니다. 이 예제에서는 플래그 변수를 사용하여 이벤트에서 연속 이벤트를 방지하는 방법을 보여 줍니다 TextChanged .

private:
   bool flag;

private:
   void MyTextChangedHandler( System::Object^ sender, System::EventArgs^ e )
   {
      Int64 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 ( 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 == 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 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

설명

속성에 빈 문자열을 할당하는 대신 이 메서드를 Text 사용하여 컨트롤의 내용을 지울 수 있습니다.

적용 대상

추가 정보