.NET2.0:MaskedTextBoxコントロール
.NET2.0で追加されたWindowsフォームコントロールである
「MaskedTextBoxコントロール」を利用したサンプルです。
MaskedTextBoxは値のValidation機能が便利です。
サンプルでは、日付を正しく入力するとラベルに「OK」の文字を表示し、
正しくない場合は「NG」の文字を表示しています。
画像は、画面を縦に3つ繋げたもので、上から順に、
- 起動直後の画面
- 正しい日付を入力した場合
- 正しくない日付を入力した場合
です。
「System.Windows.Forms」の基本的な使い方はhttp://winscript.s41.xrea.com/mt/archives/2005/09/post_4.htmlを参考にしています。
MaskedTextBoxコントロールで日付を扱う
# MaskedTextBox.msh # MaskedTextBoxのValidationメソッド function ValidateText { if ($mText.ValidateText() -eq $null){$label.Text = "NG"} else {$label.Text = "OK"} } # 初期処理 [void] [Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") [void] [System.Windows.Forms.Application]::EnableVisualStyles() # Form $form = new-object System.Windows.Forms.Form $form.Size = new-object System.Drawing.Size(150, 55) # MaskedTextBox $mText = new-object System.Windows.Forms.MaskedTextBox $mText.Size = new-object System.Drawing.Size(100, 20) $mText.Location = new-object System.Drawing.Point(0, 0) $mText.Mask = "0000年90月90日" $mText.ValidatingType = [System.DateTime] # MaskedTextBoxのTextChangedイベント $mText.Add_TextChanged({ValidateText}) # Label $label = new-object System.Windows.Forms.Label $label.Size = new-object System.Drawing.Size(40, 20) $label.Location = new-object System.Drawing.Point(100, 0) $label.TextAlign = [System.Drawing.ContentAlignment]::MiddleCenter $label.Text = "" # コントロール配置、Form表示 $form.Controls.Add($mText) $form.Controls.Add($label) $form.showDialog()