VB6のUpDownコントロールのように動作するNumericUpDownコントロール

木曜日 , 16, 5月 2019 Leave a comment

VB6のUpDownコントロールのように、Valueプロパティの値がMinimumプロパティの値より小さい値になったらMaximumプロパティの値に、Valueプロパティの値がMaximumプロパティの値より大きい値になったらMinimumプロパティの値に折り返すNumericUpDownコントロールです。

UpDownコントロールのWarpプロパティをONにした際の挙動のみを拡張しているので、それ以外の部分は必要に応じて実装してください。

  • 確認した環境
    Windows 10 Ver. 1809 (ビルド  17763)
    Visual Studio Professional 2017 Ver. 15.9.11
  • ライセンス
    NYSL ( http://www.kmonos.net/nysl/ )
  • 折り返し(Wrap)に対応したNumericUpDownコントロール

' This software is distributed under the license of NYSL.
' ( http://www.kmonos.net/nysl/ )

''' <summary>
''' VB6のUpDownのように値の折り返しに対応したNumericUpDown
''' </summary>
Public Class WrapNumericUpDown
    Inherits NumericUpDown

    Private _wrap As Boolean = False
    Private _minimum As Decimal
    Private _maximum As Decimal

    ''' <summary>
    ''' 最小値
    ''' </summary>
    ''' <returns></returns>
    Public Shadows Property Minimum As Decimal
        Get
            Return _minimum
        End Get
        Set(value As Decimal)
            _minimum = value
            MyBase.Minimum = value - 1
        End Set
    End Property

    ''' <summary>
    ''' 最大値
    ''' </summary>
    ''' <returns></returns>
    Public Shadows Property Maximum As Decimal
        Get
            Return _maximum
        End Get
        Set(value As Decimal)
            _maximum = value
            MyBase.Maximum = value + 1
        End Set
    End Property

    ''' <summary>
    ''' 折り返しフラグ
    ''' Trueの場合、Valueプロパティの値がMinimumプロパティの値より小さい値になったらMaximumプロパティの値に、Valueプロパティの値がMaximumプロパティの値より大きい値になったらMinimumプロパティの値に調整
    ''' </summary>
    ''' <returns></returns>
    Public Property Wrap As Boolean
        Get
            Return _wrap
        End Get
        Set(value As Boolean)
            _wrap = value
        End Set
    End Property

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="e"></param>
    Protected Overrides Sub OnValueChanged(e As EventArgs)

        If _wrap Then
            If Me.Value <= MyBase.Minimum Then
                Me.Value = Me.Maximum
            ElseIf Me.Value >= MyBase.Maximum Then
                Me.Value = Me.Minimum
            End If
        Else
            If Me.Value < Me.Minimum Then
                Me.Value = Me.Minimum
            ElseIf Me.Value > Me.Maximum Then
                Me.Value = Me.Maximum
            End If
        End If

        MyBase.OnValueChanged(e)

    End Sub
End Class

Please give us your valuable comment

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

このサイトはスパムを低減するために Akismet を使っています。コメントデータの処理方法の詳細はこちらをご覧ください