(VB.Net)時間用コンボボックスのユーザコントロールを作ってみた
業務で必要だったので時間入力用コンボボックスをユーザコントロールで作ってみました。
機能は
コンボボックスでプロパティで指定された区切り時間を選択できる。
直接入力も可能。(その場合は、入力チェックを行う)
直接入力とドロップダウンリストのみで選ぶことが選択可能。
というくらいの機能です。
機能やプロパティ、イベントの定義は最小限にしています。
ユーザコントロール TimeComboBox クラスを作り、デザイナでForms.ComboBoxを "combo" という名前で配置します。
下記がソースです。(文字数の関係上ハイライトはしていません。。)
Imports System.ComponentModel
'''
''' 時間入力用のコンボボックス。
'''
'''
Public Class TimeComboBox
'''
''' コンボボックスの時間の単位
'''
'''
'''
5分単位 FiveMinute = 5
'''
10分単位 TenMinute = 10
'''
15分単位 FifteenMinute = 15
'''
20分単位 TwentyMinute = 20
'''
25分単位 TwentyFiveMinute = 25
'''
30分単位 ThirtyMinute = 30
'''
1時間単位 SixtyMinute = 60
End Enum
'コンボボックスの時間単位
Private m_ComboTimeSpan As TimeSpan_Enum = TimeSpan_Enum.ThirtyMinute
'時間の値
Private m_BeforeValue As DateTime
'''
''' コンボボックスで選択、もしくは入力された時間の値を取得、設定する。
''' (年月日部分は無効な情報なので、無視すること)
'''
'''
'''
'''
Public Property Value() As DateTime
Get
check()
Return m_BeforeValue
End Get
Set(ByVal value As DateTime)
Me.combo.Text = value.Hour.ToString("0") & ":" & value.Minute.ToString("00")
End Set
End Property
'''
''' コンボボックスに表示する時間の間隔
'''
'''
'''
'''
Public Property ComboTimeSpan() As TimeSpan_Enum
Get
Return m_ComboTimeSpan
End Get
Set(ByVal value As TimeSpan_Enum)
m_ComboTimeSpan = value
InitComboList()
End Set
End Property
'''
''' ドロップダウンのスタイル
'''
'''
'''
'''
Public Property DropDownStyle() As ComboBoxStyle
Get
Return Me.combo.DropDownStyle
End Get
Set(ByVal value As ComboBoxStyle)
Me.combo.DropDownStyle = value
End Set
End Property
'''
''' コンボボックスの値を指定された時間間隔でセットする。
'''
'''
Private Sub InitComboList()
Dim max As Integer = 1440 / Me.m_ComboTimeSpan - 1
Dim comboItem(max) As String
For i As Integer = 0 To max
Dim minute As Integer = i * Me.m_ComboTimeSpan
Dim h As Integer = CInt(Math.Floor(minute / 60))
Dim m As Integer = minute Mod 60
comboItem(i) = h.ToString("0") & ":" & m.ToString("00")
Next
Me.combo.Items.Clear()
Me.combo.Items.AddRange(comboItem)
End Sub
'''
''' コンボボックスで選択されたとき
'''
'''
'''
'''
Private Sub combo_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles combo.SelectedIndexChanged
Dim dtm As DateTime
If DateTime.TryParse(combo.Text, dtm) Then
Me.m_BeforeValue = dtm
End If
End Sub
'''
''' Validated
'''
'''
'''
'''
Private Sub combo_Validated(ByVal sender As Object, ByVal e As System.EventArgs) Handles combo.Validated
'イベント発生
RaiseEvent ValueChanged(Me, New EventArgs())
End Sub
'''
''' Validating
'''
'''
'''
'''
Private Sub combo_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles combo.Validating
If Me.combo.SelectedIndex < 0 Then
If Not check() Then
MessageBox_Rhythm.Show("時間の入力の形式が間違っています。" & vbNewLine & "正しい時間の形式で入力してください。", EnumDef.MessageBoxIconMode.Warn)
combo.Text = Me.m_BeforeValue.Hour.ToString("0") & ":" & Me.m_BeforeValue.Minute.ToString("00")
e.Cancel = True
End If
End If
End Sub
'''
''' 入力チェック。値がOKならが、メンバ変数m_BeforeValueと、コンボボックスのTextプロパティにセットする。
'''
'''
'''
Private Function check() As Boolean
Dim dtm As DateTime
If DateTime.TryParse(combo.Text, dtm) Then
Me.m_BeforeValue = dtm
Me.combo.Text = dtm.Hour.ToString("0") & ":" & dtm.Minute.ToString("00")
Return True
Else
Return False
End If
End Function
#Region "イベント定義"
'''
Valueプロパティが変更されたときに発生するイベント
Category("プロパティ変更")> _
Public Event ValueChanged(ByVal sender As Object, ByVal e As System.EventArgs)
#End Region
End Class
外観はこんな感じになります。