TextBoxで入力制御の例を説明します。
仕様説明
・[↑][↓]は前・次のコントロールに移動する
・[TAB]は機能しない
・[ESC]は入力初期値に戻す
・確定は[Enter]とする
・マウス移動は入力キャンセルとする
・セットフォーカスは全選択とする
金額入力の例
Private Sub TextBox1_Enter(sender As Object, e As EventArgs) Handles TextBox1.Enter DirectCast(sender, TextBox).ClearUndo() DirectCast(sender, TextBox).SelectAll() End Sub Private Sub TextBox1_Leave(sender As Object, e As EventArgs) Handles TextBox1.Leave DirectCast(sender, TextBox).Undo() End Sub Private Sub TextBox1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox1.KeyPress If e.KeyChar = ControlChars.Back Then Return End If If Not Char.IsDigit(e.KeyChar) Then e.Handled = True End If Dim s As String = StrConv(e.KeyChar, VbStrConv.Narrow) e.KeyChar = s(0) End Sub Private Sub TextBox1_PreviewKeyDown(sender As Object, e As PreviewKeyDownEventArgs) Handles TextBox1.PreviewKeyDown Select Case e.KeyCode Case Keys.Up Me.ProcessTabKey(False) Case Keys.Down Me.ProcessTabKey(True) Case Keys.Tab e.IsInputKey = True Case Keys.Escape DirectCast(sender, TextBox).Undo() DirectCast(sender, TextBox).ClearUndo() Case Keys.Enter Dim val As Integer Dim bc As Boolean = Integer.TryParse(DirectCast(sender, TextBox).Text, System.Globalization.NumberStyles.Number, System.Globalization.CultureInfo.InvariantCulture, val) DirectCast(sender, TextBox).Text = val.ToString("#,0") DirectCast(sender, TextBox).ClearUndo() Me.ProcessTabKey(True) e.IsInputKey = True End Select End Sub
実数入力例
Private Sub TextBox1_Enter(sender As Object, e As EventArgs) Handles TextBox1.Enter DirectCast(sender, TextBox).ClearUndo() DirectCast(sender, TextBox).SelectAll() End Sub Private Sub TextBox1_Leave(sender As Object, e As EventArgs) Handles TextBox1.Leave DirectCast(sender, TextBox).Undo() End Sub Private Sub TextBox1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox1.KeyPress If e.KeyChar = ControlChars.Back Then Return End If Dim s As String = StrConv(e.KeyChar, VbStrConv.Narrow) If Not Char.IsDigit(e.KeyChar) And s <> "." Then e.Handled = True End If e.KeyChar = s(0) End Sub Private Sub TextBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyDown Select Case e.KeyCode Case Keys.Up Me.ProcessTabKey(False) Case Keys.Down Me.ProcessTabKey(True) Case Keys.Tab e.Handled = False Case Keys.Escape DirectCast(sender, TextBox).Undo() DirectCast(sender, TextBox).ClearUndo() Case Keys.Enter Dim val As Decimal Dim bc As Boolean = Decimal.TryParse(DirectCast(sender, TextBox).Text, System.Globalization.NumberStyles.Number, System.Globalization.CultureInfo.InvariantCulture, val) DirectCast(sender, TextBox).Text = val.ToString("#,0.00") DirectCast(sender, TextBox).ClearUndo() Me.ProcessTabKey(True) e.Handled = False End Select End Sub
日付入力例
Private Sub TextBox2_Enter(sender As Object, e As EventArgs) Handles TextBox2.Enter DirectCast(sender, TextBox).ClearUndo() DirectCast(sender, TextBox).SelectAll() End Sub Private Sub TextBox2_Leave(sender As Object, e As EventArgs) Handles TextBox2.Leave DirectCast(sender, TextBox).Undo() End Sub Private Sub TextBox2_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox2.KeyPress If e.KeyChar = ControlChars.Back Then Return End If Dim s As String = StrConv(e.KeyChar, VbStrConv.Narrow) If Not Char.IsDigit(s(0)) And s <> "/" Then e.Handled = True End If e.KeyChar = s(0) End Sub Private Sub TextBox2_PreviewKeyDown(sender As Object, e As PreviewKeyDownEventArgs) Handles TextBox2.PreviewKeyDown Select Case e.KeyCode Case Keys.Up Me.ProcessTabKey(False) Case Keys.Down Me.ProcessTabKey(True) Case Keys.Tab e.IsInputKey = True Case Keys.Escape DirectCast(sender, TextBox).Undo() DirectCast(sender, TextBox).ClearUndo() Case Keys.Enter Dim s As String = DirectCast(sender, TextBox).Text Dim val As Integer If Integer.TryParse(s, System.Globalization.NumberStyles.Number, System.Globalization.CultureInfo.InvariantCulture, val) Then s = Format(val, "####/00/00") End If Dim dt As DateTime If Not DateTime.TryParse(s, New System.Globalization.CultureInfo("ja-JP"), System.Globalization.DateTimeStyles.AssumeLocal, dt) Then Return End If DirectCast(sender, TextBox).Text = dt.ToString("yyyy/MM/dd") DirectCast(sender, TextBox).ClearUndo() Me.ProcessTabKey(True) e.IsInputKey = True End Select End Sub
※上記の例では、クリップボードの制御をしていません。
・上記の例ではKeyDownとPreviewKeyDownを使用していますが、通常KeyDownでよいようです。PreviewKeyDownはButtonなどの時に使用します。