Author Topic: ToolStrip 相關的程序函數庫  (Read 7603 times)

admin

  • Administrator
  • *****
  • Posts: 0
    • View Profile
ToolStrip 相關的程序函數庫
« on: August 18, 2012, 06:03:54 PM »
Code: [Select]
Module modToolStrip
    Public Sub initToolBar(ByRef tmpToolStrip As ToolStrip)
        On Error GoTo ErrHandler
        Dim tmpToolStripItem As ToolStripItem
        Dim tmpMargin As New Padding(0, 0, 0, 0)
        For Each tmpToolStripItem In tmpToolStrip.Items
            If TypeOf (tmpToolStripItem) Is ToolStripButton Then
                tmpToolStripItem.ImageScaling = ToolStripItemImageScaling.None
                tmpToolStripItem.Margin = tmpMargin
            End If
        Next

        Exit Sub

ErrHandler:
        MessageBox.Show("initToolBar Module Error : " & Err.Number & " - " & Err.Description, "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        Resume Next

    End Sub

    Private Function ReverseString(ByVal tmpData As String, ByVal tmpButtonQty As Integer) As String
        Dim tmpResult As String = String.Empty
        Dim intX As Integer
        For intX = tmpData.Length To 1 Step -1
            tmpResult = tmpResult + Mid(tmpData, intX, 1)
        Next

        While tmpResult.Length < tmpButtonQty
            tmpResult = "0" + tmpResult
        End While

        Return tmpResult
    End Function

    Private Function GetToolStripButtonQty(ByVal tmpToolStrip As ToolStrip) As Integer
        Dim tmpResult As Integer = 0
        Dim tmpToolStripItem As ToolStripItem
        For Each tmpToolStripItem In tmpToolStrip.Items
            If TypeOf (tmpToolStripItem) Is ToolStripButton Then
                tmpResult = tmpResult + 1

            End If
        Next

        Return tmpResult
    End Function

    Public Sub ArrangeToolButton(ByRef tmpToolStrip As ToolStrip, Optional ByVal ShowWhichButton As String = "11111")
        Dim tmpToolStripItem As ToolStripItem
        Dim tmpCountPos As Integer = 0
        Dim tmpReverseString As String
        Dim tmpButtonQty As Integer
        tmpButtonQty = GetToolStripButtonQty(tmpToolStrip)
        tmpReverseString = ReverseString(ShowWhichButton, tmpButtonQty)
        For Each tmpToolStripItem In tmpToolStrip.Items
            If TypeOf (tmpToolStripItem) Is ToolStripButton Then
                tmpCountPos = tmpCountPos + 1
                If tmpCountPos <= Len(tmpReverseString) Then
                    If Val(Mid(tmpReverseString, tmpCountPos, 1)) = 1 Then
                        tmpToolStripItem.Visible = True
                    Else
                        tmpToolStripItem.Visible = False
                    End If

                Else
                    tmpToolStripItem.Visible = False
                End If
            End If
        Next
    End Sub


    Public Sub SwitchConfirmButton(ByRef tmpToolStrip As ToolStrip, Optional ByVal ConfirmButtons As Integer = 2)
        Dim tmpToolStripItem As ToolStripItem
        Dim tmpCountPos As Integer = 0
        Dim tmpReverseString As String
        Dim tmpButtonQty As Integer
        Dim tmpShowWhichButton As String = String.Empty
        Dim intX As Integer
        tmpButtonQty = GetToolStripButtonQty(tmpToolStrip)

        For intX = 1 To tmpButtonQty - ConfirmButtons
            tmpShowWhichButton = tmpShowWhichButton + "0"
        Next
        For intX = 1 To ConfirmButtons
            tmpShowWhichButton = tmpShowWhichButton + "1"
        Next

        tmpReverseString = ReverseString(tmpShowWhichButton, tmpButtonQty)
        For Each tmpToolStripItem In tmpToolStrip.Items
            If TypeOf (tmpToolStripItem) Is ToolStripButton Then
                tmpCountPos = tmpCountPos + 1
                If tmpCountPos <= Len(tmpReverseString) Then
                    If Val(Mid(tmpReverseString, tmpCountPos, 1)) = 1 Then
                        tmpToolStripItem.Visible = True
                    Else
                        tmpToolStripItem.Visible = False
                    End If

                Else
                    tmpToolStripItem.Visible = False
                End If
            End If
        Next
    End Sub

    Public Function AutoWrapText(ByVal tmpText As String) As String
        Dim intX As Integer
        Dim tmpWrapPos As Integer
        Dim tmpResult As String = String.Empty
        If Len(tmpText) > 2 Then
            Select Case Len(tmpText)
                Case Is < 5
                    tmpWrapPos = 2

                Case Is >= 5
                    tmpWrapPos = 3

            End Select


            For intX = 1 To Len(tmpText)
                tmpResult = tmpResult + Mid(tmpText, intX, 1)
                If intX = tmpWrapPos Then
                    tmpResult = tmpResult + vbCrLf
                End If

            Next


        End If

        Return tmpResult

    End Function

End Module