TextBoxBase.Clear メソッド
アセンブリ: 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

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


Weblioに収録されているすべての辞書からTextBoxBase.Clear メソッドを検索する場合は、下記のリンクをクリックしてください。

- TextBoxBase.Clear メソッドのページへのリンク