次の方法で共有


TextBoxBase.Clear メソッド

テキスト ボックス コントロールからすべてのテキストを削除します。

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

解説

このメソッドを使用すると、 Text プロパティに空の文字列を代入せずに、コントロールの内容を削除できます。

使用例

[Visual Basic, C#, C++] 派生クラス TextBox を使用して、 TextChanged イベントのイベント ハンドラを作成する例を次に示します。イベント ハンドラ内のコードでは、データは数値に制限されます。コントロールにテキストが入力されると、コードは入力されたテキストが数値かどうかを判断します。テキストが数値ではない場合、コードによりテキストがコントロールから削除され、数値だけが入力できることをユーザーに通知する MessageBox が表示されます。この例は、 flag という名前の Boolean 変数と textBox1 という名前の TextBox コントロールが、このメソッド以外で定義されていることを前提にしています。この例では、flag 変数を使用して TextChanged イベントで連鎖イベントが発生しないようにする方法を示します。

 
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


[C#] 
private bool flag;

public 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;
    }        
 }
 

[C++] 
private:
bool flag;

public:
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(S"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;
    }        
 }
 

[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ

参照

TextBoxBase クラス | TextBoxBase メンバ | System.Windows.Forms 名前空間 | Cut | Copy | Paste | CanUndo | ClearUndo