Imports System
Imports System.ComponentModel
Imports System.Windows.Forms
Imports C1.Win.C1Ribbon

''' <summary>
''' The Ribbon element that contains a NumericUpDown.
''' </summary>
Public Class NumericUpDownHost
    Inherits RibbonControlHost

    ''' <summary>
    ''' Creates the NumericUpDown control and its host element.
    ''' </summary>
    Public Sub New()
        MyBase.New(New NumericUpDown())
    End Sub

    ''' <summary>
    ''' Gets the hosted NumericUpDown control.
    ''' </summary>
    <Browsable(False)> _
    <DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)> _
    Public ReadOnly Property NumericUpDown() As NumericUpDown
        Get
            Return CType(Control, NumericUpDown)
        End Get
    End Property

    ''' <summary>
    ''' Gets or sets the value assigned to the NumericUpDown.
    ''' </summary>
    <Category("Data")> _
    <Description("Gets or sets the value assigned to the NumericUpDown.")> _
    Public Property Value() As System.Decimal
        Get
            Return NumericUpDown.Value
        End Get
        Set(ByVal value As System.Decimal)
            NumericUpDown.Value = value
        End Set
    End Property

    ''' <summary>
    ''' Gets or sets the minimum value for the NumericUpDown.
    ''' </summary>
    <Category("Behavior")> _
    <Description("Gets or sets the minimum value for the NumericUpDown.")> _
    <DefaultValue(0)> _
    Public Property Minimum() As System.Decimal
        Get
            Return NumericUpDown.Minimum
        End Get
        Set(ByVal value As System.Decimal)
            NumericUpDown.Minimum = value
        End Set
    End Property

    ''' <summary>
    ''' Gets or sets the maximum value for the NumericUpDown.
    ''' </summary>
    <Category("Behavior")> _
    <Description("Gets or sets the maximum value for the NumericUpDown.")> _
    <DefaultValue(100)> _
    Public Property Maximum() As System.Decimal
        Get
            Return NumericUpDown.Maximum
        End Get
        Set(ByVal value As System.Decimal)
            NumericUpDown.Maximum = value
        End Set
    End Property

    ''' <summary>
    ''' Gets or sets the value to increment or decrement the NumericUpDown when the up or down buttons are clicked.
    ''' </summary>
    <Category("Behavior")> _
    <Description("Gets or sets the value to increment or decrement the NumericUpDown when the up or down buttons are clicked.")> _
    <DefaultValue(1)> _
    Public Property Increment() As System.Decimal
        Get
            Return NumericUpDown.Increment
        End Get
        Set(ByVal value As System.Decimal)
            NumericUpDown.Increment = value
        End Set
    End Property

    ''' <summary>
    ''' Gets or sets the text that appears on the NumericUpDown.
    ''' </summary>
    <Browsable(False)> _
    <DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)> _
    Public Overrides Property Text() As String
        Get
            Return MyBase.Text
        End Get
        Set(ByVal value As String)
            MyBase.Text = value
        End Set
    End Property

    ''' <summary>
    ''' Occurs when the <see cref="Value"/> property changes.
    ''' </summary>
    <Category("Property Changed")> _
    <Description("Occurs when the Value property changes.")> _
    Public Event ValueChanged As EventHandler
    ''' <summary>
    ''' Raises the <see cref="ValueChanged"/> event.
    ''' </summary>
    Protected Overridable Sub OnValueChanged(ByVal e As EventArgs)
        RaiseEvent ValueChanged(Me, e)
    End Sub

    Protected Overrides Sub OnSubscribeControlEvents(ByVal ctrl As Control)
        MyBase.OnSubscribeControlEvents(ctrl)
        AddHandler CType(ctrl, NumericUpDown).ValueChanged, AddressOf HandleValueChanged
    End Sub
    Protected Overrides Sub OnUnsubscribeControlEvents(ByVal ctrl As Control)
        MyBase.OnUnsubscribeControlEvents(ctrl)
        RemoveHandler CType(ctrl, NumericUpDown).ValueChanged, AddressOf HandleValueChanged
    End Sub

    Private Sub HandleValueChanged(ByVal sender As Object, ByVal e As EventArgs)
        Me.OnValueChanged(e)
    End Sub

End Class
