http://www.21code.com/codebase/?pos=list&type=search&key=类

解决方案 »

  1.   

    以下是一个list类,你可以比较一下,如果你有 做过控件,不难发现两者有共同点=======================================Option ExplicitPrivate mvarNewIndex As Long 'local copy
    Private mvarListCount As Long 'local copy
    Private mvarListIndex As Long 'local copy
    Private mvarSorted As Boolean 'local copy
    Private mvarSelText As String 'local copy
    Private mvarTopIndex As Long 'local copyPrivate mvarList() As String
    Private mvarItemData() As String
    Private mvarItemSelected() As Boolean
    Private mvarErr As String 'local copy
    Public Property Let Err(ByVal vData As String)
        mvarErr = vData
    End Property
    Public Property Get Err() As String
        Err = mvarErr
    End PropertyPrivate Sub Class_Initialize()
        ReDim Preserve mvarList(0)
        ReDim Preserve mvarItemData(0)
        ReDim Preserve mvarItemSelected(0)
        
        mvarListCount = -1
    End SubPublic Sub RemoveItem(ByVal Index As Long)
        If vData <= UBound(mvarList) And vData >= LBound(mvarList) Then
            Dim NmvarList() As String
            Dim NmvarItemData() As String
            Dim NmvarItemSelected() As String
            
            ReDim Preserve NmvarList(mvarListCount - 2)
            ReDim Preserve NmvarItemData(mvarListCount - 2)
            ReDim Preserve NmvarItemSelected(mvarListCount - 2)
            Dim i As Integer
            For i = 0 To Index - 1
                NmvarList(i) = mvarList(i)
                NmvarItemData(i) = mvarItemData(i)
                NmvarItemSelected(i) = mvarItemSelected(i)
            Next i
            If Index < mvarListCount - 1 Then
                For i = Index + 1 To mvarListCount - 1
                    NmvarList(i - 1) = mvarList(i)
                    NmvarItemData(i - 1) = mvarItemData(i)
                    NmvarItemSelected(i - 1) = mvarItemSelected(i)
                Next i
            End If
            Erase mvarList
            Erase mvarItemData
            Erase mvarItemSelected
            mvarList = NmvarList
            mvarItemData = mvarItemData
            mvarItemSelected = mvarItemSelected
            
            mvarListCount = mvarListCount - 1
        Else
            mvarErr = "Invalid Index!"
            Exit Property
        End If
    End SubPublic Property Let TopIndex(ByVal vData As Long)
        mvarTopIndex = vData
    End Property
    Public Property Get TopIndex() As Long
        If vData <= UBound(mvarList) And vData >= LBound(mvarList) Then
            TopIndex = mvarTopIndex
        Else
            mvarErr = "Invalid Index!"
            Exit Property
        End IfEnd PropertyPublic Property Let SelText(ByVal vData As String)
        mvarSelText = vData
    End Property
    Public Property Get SelText() As String
        SelText = mvarSelText
    End PropertyPublic Property Let Sorted(ByVal vData As Boolean)
        mvarSorted = vData
    End Property
    Public Property Get Sorted() As Boolean
        Sorted = mvarSorted
    End PropertyPublic Property Let ListIndex(ByVal vData As Long)
        
        If vData <= UBound(mvarList) Then
            mvarListIndex = vData
        Else
            mvarErr = "Invalid Index!"
            Exit Property
        End If
    End Property
    Public Property Get ListIndex() As Long
        ListIndex = mvarListIndex
        
    End Property
    Public Property Get ListCount() As Long
        ListCount = mvarListCount
    End PropertyPublic Sub Clear()   Erase mvarList
       Erase mvarItemData
       Erase mvarItemSelected
       
       ReDim Preserve mvarList(0)
       ReDim Preserve mvarItemData(0)
       ReDim Preserve mvarItemSelected(0)   mvarListCount = -1
    End SubPublic Sub AddItem(ByVal Item As String, Optional ByVal Index As Long)
       
       If Not IsMissing(Index) Then
           If Index <> mvarListCount + 1 Then
                mvarErr = "Invalid Index!"
                Exit Function
           End If
       End If
       If mvarListCount <> -1 Then
            ReDim Preserve mvarList(mvarListCount + 1)
            ReDim Preserve mvarItemData(mvarListCount + 1)
            ReDim Preserve mvarItemSelected(mvarListCount + 1)
       End If
       
       mvarListCount = mvarListCount + 1
       mvarNewIndex = mvarListCount
       
       mvarList(mvarListCount) = Item
       mvarItemData(mvarListCount) = 0
       mvarItemSelected(mvarListCount) = False
        
    End SubPublic Property Let List(ByVal Index As Long, ByVal vData As String)
        
        AddItem vData, IndexEnd Property
    Public Property Get List(ByVal Index As Long) As String    If Index > UBound(mvarList) Then
            mvarErr = "Invalid Index!"
            Exit Property
        End If
        List = mvarList(Index)End Property
    Public Property Get NewIndex() As Long
        NewIndex = mvarNewIndex
    End Property
    Public Property Let ItemData(ByVal Index As Long, ByVal vData As String)
        If Index <= UBound(mvarList) Then
            mvarItemData(Index) = vData
        Else
            mvarErr = "Invalid Index!"
            Exit Property
        End If
    End Property
    Public Property Get ItemData(ByVal Index As Long) As String    If Index <= UBound(mvarList) Then
            ItemData = mvarItemData(Index)
        Else
            mvarErr = "Invalid Index!"
            Exit Property
        End If
        
    End PropertyPublic Property Let ItemSelected(ByVal Index As Long, ByVal vData As Boolean)
        If Index <= UBound(mvarList) Then
            mvarItemSelected(Index) = vData
        Else
            mvarErr = "Invalid Index!"
            Exit Property
        End If
    End Property
    Public Property Get ItemSelected(ByVal Index As Long) As Boolean    If Index <= UBound(mvarList) Then
            ItemSelected = mvarItemSelected(Index)
        Else
            mvarErr = "Invalid Index!"
            Exit Property
        End If
        
    End Property