TextBoxBase.Clear メソッドとは? わかりやすく解説

TextBoxBase.Clear メソッド

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

名前空間: System.Windows.Forms
アセンブリ: System.Windows.Forms (system.windows.forms.dll 内)
構文構文

解説解説
使用例使用例

派生クラス 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

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:
   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 boolean flag;
private void MyTextChangedHandler(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.get_Text());
        }
        catch (System.Exception exp) {
            // 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;
    }
} //MyTextChangedHandler
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照


このページでは「.NET Framework クラス ライブラリ リファレンス」からTextBoxBase.Clear メソッドを検索した結果を表示しています。
Weblioに収録されているすべての辞書からTextBoxBase.Clear メソッドを検索する場合は、下記のリンクをクリックしてください。
 全ての辞書からTextBoxBase.Clear メソッド を検索

英和和英テキスト翻訳>> Weblio翻訳
英語⇒日本語日本語⇒英語
  

辞書ショートカット

すべての辞書の索引

TextBoxBase.Clear メソッドのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



TextBoxBase.Clear メソッドのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2025 Microsoft.All rights reserved.

©2025 GRAS Group, Inc.RSS