Author Topic: ComboBox 相關的程序函數庫  (Read 9468 times)

admin

  • Administrator
  • *****
  • Posts: 0
    • View Profile
ComboBox 相關的程序函數庫
« on: August 18, 2012, 06:11:10 PM »
Code: [Select]
Option Explicit On
Imports System.Windows.Forms.Control
Module modComboBox

    Public gblnSetfocus As Boolean

    Public Class ComboBoxItem
        Public ItemValue As Int32
        Public ItemDescription As String

        Public Sub New(ByVal NewValue As Int32, ByVal NewDescription As String)
            ItemValue = NewValue
            ItemDescription = NewDescription
        End Sub

        Public Overrides Function ToString() As String
            Return ItemDescription
        End Function

    End Class


    Public Sub FillComboItem(ByRef tmpCombo As ComboBox, ByVal inputList As String, ByVal inputKey As String)
        On Error GoTo ErrHandler
        If (Len(inputList) > 0 And Len(inputKey) > 0) Then
            tmpCombo.Items.Add(New ComboBoxItem(Val(inputKey), inputList))
        End If

ErrHandler:


    End Sub

    Public Sub FillComboInt(ByRef tmpCombo As ComboBox, ByVal inputSQL As String, ByVal inputList As String, ByVal inputKey As String, Optional ByVal NoClear As Boolean = False)
        On Error GoTo ErrHandler
        If NoClear <> True Then tmpCombo.Items.Clear()

        If CreateDBReader(inputSQL, grsModule) = True Then
            While grsModule.read
                If Len(CStr(grsModule(inputList).ToString)) > 0 Then
                    tmpCombo.Items.Add(New ComboBoxItem(Val(grsModule(inputKey)), CStr(grsModule(inputList))))
                End If
            End While
            grsModule.Close()
        End If

        Exit Sub

ErrHandler:
        Resume Next

    End Sub



    Public Sub ShowCombobyItem(ByVal tmpCombo As ComboBox, ByVal inputValue As String, Optional ByVal tmpReset As Boolean = True)
        On Error GoTo ErrShowComboByItem

        Dim intX As Integer
        Dim tmpLastStatus As Boolean
        tmpLastStatus = gblnSetfocus
        gblnSetfocus = True
        If Len(inputValue) > 0 Then
            For intX = 0 To tmpCombo.Items.Count - 1
                If CType(tmpCombo.Items(intX), ComboBoxItem).ItemValue = inputValue Then
                    tmpCombo.SelectedIndex = intX
                    gblnSetfocus = tmpLastStatus
                    Exit Sub
                End If
            Next
            If tmpReset = True Then tmpCombo.SelectedIndex = -1
        Else
            If tmpReset = True Then tmpCombo.SelectedIndex = -1
        End If
        gblnSetfocus = tmpLastStatus
        Exit Sub

ErrShowComboByItem:
        tmpCombo.SelectedIndex = -1

    End Sub

    Public Sub ShowComboByTxt(ByVal tmpCombo As ComboBox, ByVal inputValue As String, Optional ByVal tmpReset As Boolean = True)
        On Error GoTo ErrShowComboByTxt
        Dim intX As Integer
        Dim tmpLastStatus As Boolean
        tmpLastStatus = gblnSetfocus
        gblnSetfocus = True
        If Len(inputValue) > 0 Then
            For intX = 0 To tmpCombo.Items.Count - 1
                'If CType(tmpCombo.Items(intX), ComboBoxItem).ItemValue = inputValue Then
                If tmpCombo.GetItemText(tmpCombo.Items(intX)) = inputValue Then
                    tmpCombo.SelectedIndex = intX
                    gblnSetfocus = tmpLastStatus
                    Exit Sub
                End If
            Next
            If tmpReset = True Then tmpCombo.SelectedIndex = -1
        Else
            If tmpReset = True Then tmpCombo.SelectedIndex = -1
        End If
        gblnSetfocus = tmpLastStatus
        Exit Sub

ErrShowComboByTxt:
        tmpCombo.SelectedIndex = -1

    End Sub

    Public Sub ShowCombo(ByVal tmpCombo As ComboBox, ByVal inputValue As String, Optional ByVal tmpSetFocus As Boolean = True)
        On Error GoTo ErrShowCombo
        Dim tmpLastStatus As Boolean
        tmpLastStatus = gblnSetfocus
        gblnSetfocus = tmpSetFocus
        tmpCombo.Text = inputValue
        gblnSetfocus = tmpLastStatus
        Exit Sub

ErrShowCombo:
        tmpCombo.SelectedIndex = -1

    End Sub

End Module