Imports System
Imports System.Windows.Forms
Imports System.Runtime.CompilerServices

Namespace TUSB0214DAMSampleAppVB
    Module ExtendByte
        <Extension()>
        Public Function IsBitSet0(b As Byte) As Boolean
            Return (b And &H1) <> 0
        End Function

        <Extension()>
        Public Function IsBitSet1(b As Byte) As Boolean
            Return (b And &H2) <> 0
        End Function

        <Extension()>
        Public Function IsBitSet2(b As Byte) As Boolean
            Return (b And &H4) <> 0
        End Function

        <Extension()>
        Public Function IsBitSet3(b As Byte) As Boolean
            Return (b And &H8) <> 0
        End Function

        <Extension()>
        Public Function IsBitSet4(b As Byte) As Boolean
            Return (b And &H10) <> 0
        End Function

        <Extension()>
        Public Function IsBitSet5(b As Byte) As Boolean
            Return (b And &H20) <> 0
        End Function

        <Extension()>
        Public Function IsBitSet6(b As Byte) As Boolean
            Return (b And &H40) <> 0
        End Function

        <Extension()>
        Public Function IsBitSet7(b As Byte) As Boolean
            Return (b And &H80) <> 0
        End Function

        <Extension()>
        Public Sub BitSet0(ByRef b As Byte)
            b += &H1
        End Sub

        <Extension()>
        Public Sub BitSet1(ByRef b As Byte)
            b += &H2
        End Sub

        <Extension()>
        Public Sub BitSet2(ByRef b As Byte)
            b += &H4
        End Sub

        <Extension()>
        Public Sub BitSet3(ByRef b As Byte)
            b += &H8
        End Sub

        <Extension()>
        Public Sub BitSet4(ByRef b As Byte)
            b += &H10
        End Sub

        <Extension()>
        Public Sub BitSet5(ByRef b As Byte)
            b += &H20
        End Sub

        <Extension()>
        Public Sub BitSet6(ByRef b As Byte)
            b += &H40
        End Sub

        <Extension()>
        Public Sub BitSet7(ByRef b As Byte)
            b += &H80
        End Sub
    End Module

    Module ExtendDecimal
        <Extension()>
        Public Function ToUShort(d As Decimal) As UShort
            Try
                If d < UShort.MinValue Then
                    Return UShort.MinValue
                ElseIf d > UShort.MaxValue Then
                    Return UShort.MaxValue
                Else
                    Return CUShort(d)
                End If
            Catch
                Return 0
            End Try
        End Function

        <Extension()>
        Public Function ToInt(d As Decimal) As Integer
            Try
                If d < Integer.MinValue Then
                    Return Integer.MinValue
                ElseIf d > Integer.MaxValue Then
                    Return Integer.MaxValue
                Else
                    Return CInt(d)
                End If
            Catch
                Return 0
            End Try
        End Function

        <Extension()>
        Public Function ToDouble(d As Decimal) As Double
            Try
                Return CDbl(d)
            Catch
                Return 0.0
            End Try
        End Function
    End Module

    Module ExtendInt
        <Extension()>
        Public Function ToByte(i As Integer) As Byte
            Try
                If i < Byte.MinValue Then
                    Return Byte.MinValue
                ElseIf i > Byte.MaxValue Then
                    Return Byte.MaxValue
                Else
                    Return CByte(i)
                End If
            Catch
                Return 0
            End Try
        End Function
    End Module

    Module ExtendDouble
        <Extension()>
        Public Function ToUShort(d As Double) As UShort
            Try
                If d < UShort.MinValue Then
                    Return UShort.MinValue
                ElseIf d > UShort.MaxValue Then
                    Return UShort.MaxValue
                Else
                    Return CUShort(Math.Round(d, MidpointRounding.AwayFromZero))
                End If
            Catch
                Return 0
            End Try
        End Function
    End Module

    Module ExtendControls
        ' ボタンのバックカラーを注意喚起する色か通常状態の色にする
        <Extension()>
        Public Sub AwakeColor(button As Button, awake As Boolean)
            If awake Then
                button.BackColor = Color.Salmon
            Else
                button.UseVisualStyleBackColor = True
            End If
        End Sub

        ' コンボボックスのバックカラーを警報色にするか通常の色にする
        <Extension()>
        Public Function AlartColor(numericUpDown As NumericUpDown, alart As Boolean) As Boolean
            If alart Then
                numericUpDown.BackColor = Color.Red
            Else
                numericUpDown.BackColor = SystemColors.Window
            End If
            Return alart
        End Function
    End Module
End Namespace