Public Class CommonLib
Shared Sub Form_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs)
If TypeOf sender Is System.Windows.Forms.Form Then
e.Cancel = True
sender.Hide()
End If
End Sub
Shared Sub TextBox_Enter(ByVal sender As Object, ByVal e As System.EventArgs)
If TypeOf sender Is System.Windows.Forms.TextBox Then
sender.BackColor = System.Drawing.Color.LightCyan
End If
End Sub
Shared Sub TextBox_Leave(ByVal sender As Object, ByVal e As System.EventArgs)
If TypeOf sender Is System.Windows.Forms.TextBox Then
sender.BackColor = System.Drawing.Color.Empty
End If
End Sub
Shared Sub TextBox_Validating_Integer(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs)
Dim tb As System.Windows.Forms.TextBox = DirectCast(sender, System.Windows.Forms.TextBox)
If tb.Text.Length = 0 Then Exit Sub
'数値のみが入力されているかチェック
Dim tmp As Integer
If Integer.TryParse(tb.Text, tmp) Then
Else
MsgBox("数値(整数)のみ入力可能です")
tb.Text = ""
End If
End Sub
Shared Sub TextBox_Validating_Decimal(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs)
Dim tb As System.Windows.Forms.TextBox = DirectCast(sender, System.Windows.Forms.TextBox)
If tb.Text.Length = 0 Then Exit Sub
'数値のみが入力されているかチェック
Dim tmp As Decimal
If Decimal.TryParse(tb.Text, tmp) Then
Else
MsgBox("数値のみ入力可能です")
tb.Text = ""
tb.Focus()
End If
End Sub
End Class
[コードの説明]
- Form_FormClosing:フォームを閉じたときに非表示にするだけにする。フォームを再表示するときに毎回Newしなくて済むのでレスポンスup
- TextBox_Enter:TextBoxにフォーカスが移ったときにBackColorを変えてわかりやすくする。
- TextBox_Leave:フォーカスが離れたときにTextBox_Enterで変えたBackColorを取り消す。
- TextBox_Validating_Integer:TextBoxに整数のみが入力されているかチェックする。
- TextBox_Validating_Decimal:TextBoxに数値のみが入力されているかチェックする。
[使い方]
各フォームのForm_Loadイベントに
AddHandler Me.FormClosing, AddressOf CommonLib.Form_FormClosing AddHandler tb接続DBファイル.Enter, AddressOf CommonLib.TextBox_Enter AddHandler tb接続DBファイル.Leave, AddressOf CommonLib.TextBox_Leaveのように追加して使用する。