| Imports System.Windows.Forms
Public Class LboxText
Public Shared validateNone As Boolean = False
' ******************************************************
' validate コントロール用
' ******************************************************
Protected Overloads Overrides Sub OnValidating( _
ByVal e As System.ComponentModel.CancelEventArgs)
' ******************************************************
' validateNone が True ならば、Valdating は発生しない
' ******************************************************
If Not validateNone Then
MyBase.OnValidating(e)
End If
End Sub
Protected Overloads Overrides Sub OnValidated( _
ByVal e As System.EventArgs)
' ******************************************************
' validateNone が True ならば、Validated は発生しない
' ******************************************************
If Not validateNone Then
MyBase.OnValidated(e)
End If
End Sub
' ******************************************************
' カスタムデータ型
' ******************************************************
Private _DataType As Integer = 0
<System.ComponentModel.Description( _
"0:文字列" & Chr(10) & _
"1:YYYY/MM/DD" & Chr(10) & _
"2:先行ゼロ文字列(長さはMaxLengthで指定)" _
), _
System.ComponentModel.DefaultValue(0)> _
Public Property DataType() As Integer
Get
Return _DataType
End Get
Set(ByVal value As Integer)
_DataType = value
End Set
End Property
' ******************************************************
' カスタムチェックタイプ
' ******************************************************
Private _CheckType As Integer = 0
<System.ComponentModel.Description("0:なし" & Chr(10) & "1:必須入力"), _
System.ComponentModel.DefaultValue(0)> _
Public Property CheckType() As Integer
Get
Return _CheckType
End Get
Set(ByVal value As Integer)
_CheckType = value
End Set
End Property
' ******************************************************
' 編集文字列を省く
' ******************************************************
Private Sub LboxText_Enter(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Enter
If Me._DataType = 1 Then
Me.MaxLength = 8
Dim str As String = Me.Text
str = str.Replace("/", "")
Me.Text = str
End If
End Sub
' ******************************************************
' 編集する
' ******************************************************
Private Sub LboxText_Leave(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Leave
If Me._DataType = 1 Then
If Me.Text <> "" Then
Dim str As String = _
Integer.Parse(Me.Text).ToString("0000/00/00")
Me.Text = str
End If
End If
If Me._DataType = 2 Then
If Me.Text <> "" Then
If Me.MaxLength > 1000 Then
Me.MaxLength = 4
End If
Dim formatString = ""
For i As Integer = 1 To Me.MaxLength
formatString += "0"
Next
Dim str As String = _
Integer.Parse(Me.Text).ToString(formatString)
Me.Text = str
End If
End If
End Sub
' ******************************************************
' カスタム入力制限
' ******************************************************
Private _AllowChar As String = ""
<System.ComponentModel.Description("セットした文字列のみ入力可能とする"), _
System.ComponentModel.DefaultValue("")> _
Public Property AllowChar() As String
Get
Return _AllowChar
End Get
Set(ByVal value As String)
_AllowChar = value
End Set
End Property
' ******************************************************
' カスタム入力文字列値チェック
' ******************************************************
Private _CheckValue As String = ""
<System.ComponentModel.Description("カンマで区切られたそれぞれの文字列以外入力不可とする"), _
System.ComponentModel.DefaultValue("")> _
Public Property CheckValue() As String
Get
Return _CheckValue
End Get
Set(ByVal value As String)
_CheckValue = value
End Set
End Property
' ******************************************************
' 対象文字列以外は入力不可にする
' ******************************************************
Private Sub LboxText_KeyPress(ByVal sender As System.Object, _
ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles MyBase.KeyPress
If Me._DataType = 1 Or Me._DataType = 2 Then
If (ControlChars.Back + _
"0123456789").IndexOf(e.KeyChar.ToString()) < 0 Then
e.Handled = True
Else
Return
End If
End If
If Me._AllowChar <> "" Then
If (ControlChars.Back + _
Me._AllowChar).IndexOf(e.KeyChar.ToString()) < 0 Then
e.Handled = True
End If
End If
End Sub
' ******************************************************
' 全体のチェック
' ******************************************************
Private Sub LboxText_Validating(ByVal sender As System.Object, _
ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Validating
If Me._CheckType = 1 Then
If (Me.Text).Trim() = "" Then
Me.Text = ""
End If
If Me.Text = "" Then
MessageBox.Show("必須入力です ", _
"エラー", MessageBoxButtons.OK, MessageBoxIcon.Error)
e.Cancel = True
End If
End If
If Me._DataType = 1 Then
If Me.Text <> "" Then
Try
Dim dt As Date = Date.Parse(Me.Text)
Catch ex As Exception
MessageBox.Show("日付を正しく入力して下さい ", _
"エラー", MessageBoxButtons.OK, MessageBoxIcon.Error)
Me.SelectAll()
e.Cancel = True
End Try
End If
End If
If Me.Text <> "" And Me._CheckValue <> "" Then
Dim delimStr As String = ","
Dim delimiter As Char() = delimStr.ToCharArray()
Dim split As String() = Me._CheckValue.Split(delimiter)
Dim strValue As String
Dim flg As Boolean = False
For Each strValue In split
If Me.Text = strValue Then
flg = True
End If
Next
If Not flg Then
MessageBox.Show("入力された値は正しくありません ", _
"エラー", MessageBoxButtons.OK, MessageBoxIcon.Error)
Me.SelectAll()
e.Cancel = True
End If
End If
End Sub
End Class
| |