DropDown CheckedListBox问题

时间:2015-01-22 19:41:17

标签: vb.net

正在考虑为我的一个表单制作DropDownCheckedListBox。我发现了一个完整的源代码在线。

Public Class DropDownCheckedListBox
Private Const T_DisplayListSize As Integer = 6
Private Const SelectNoneText As String = "(None Selected)"
Private Const SelectAllText As String = "(All Selected)"
Private Const SelectSomeText As String = "(Some Selected...)"
Private Frm As Form
Private Shadows LostFocus As Boolean
Private CodeValue As String
Private T_MustFill As Boolean
Private Shared m_ChkItemsString As String
Public Event DropDown()
Public Shadows Event TextChanged()

Public Sub New()
    InitializeComponent()
    InitializeNew()
End Sub

Private Sub InitializeNew()
    Dim strTemp As String = Nothing
    ListSize = T_DisplayListSize
    T_DroppedDown = False
    T_ListText = ""
    T_MustFill = False
    txt.Text = strTemp
    Checklisbox.Hide()
    Frm = New Form
    With Frm
        .ShowInTaskbar = False
        .FormBorderStyle = FormBorderStyle.None
        .ControlBox = False
        .StartPosition = FormStartPosition.Manual
        .TopMost = True
        .Location = Checklisbox.Location
        .Width = Checklisbox.Width
        .Controls.Add(Checklisbox)
    End With
    SetSize()
End Sub
Private dataList() As String
Public Property Items() As String()
    Get
        Return dataList
    End Get
    Set(ByVal value As String())
        dataList = value
    End Set
End Property
Private ListSize As Integer
Public Property DisplayListSize() As Integer
    Get
        Return ListSize
    End Get
    Set(ByVal value As Integer)
        ListSize = value
        SetList()
    End Set
End Property
Private T_DroppedDown As Boolean
Public ReadOnly Property DroppedDown() As Boolean
    Get
        Return T_DroppedDown
    End Get
End Property
Private T_ListText As String
Public ReadOnly Property ListText() As String
    Get
        Return T_ListText
    End Get
End Property
Private Sub ListButtonClick()
    Dim strTemp As String
    strTemp = T_ListText
    If T_DroppedDown Then
        T_DroppedDown = False
        txt.Text = GetSelectedItems()
        Checklisbox.Hide()
        Frm.Hide()
        txt.Focus()
        If Not strTemp = T_ListText Then
            RaiseEvent TextChanged()
        End If
    ElseIf Not LostFocus Then
        T_DroppedDown = True
        SetSize()
        Frm.Show()
        Checklisbox.Show()
        Checklisbox.Focus()
        RaiseEvent DropDown()
    End If
    LostFocus = False

End Sub

Private Function GetSelectedItems() As String
    Dim strLst As String
    Dim blnAllSelected As Boolean = False
    strLst = ""
    With Checklisbox
        If .Items.Count > 0 Then
            If .CheckedIndices.Count = 0 Then
                strLst = SelectNoneText
            Else
                If .CheckedIndices.Count = .Items.Count Then
                    strLst = SelectAllText
                Else
                    strLst = .CheckedIndices.Count & " selected" 'SelectSomeText
                End If
            End If
        Else
            strLst = SelectNoneText
        End If
    End With
    Return strLst
End Function

'Removed code from this area that was just click, keystrokes events. 
'Also Removed resize code.

Public Event SelectedIndexChanged(ByVal sender As DropDownCheckedListBox)

Public Shared Function GetItemsNameString(ByVal tempListBox As CheckedListBox) As String
    m_ChkItemsString = ""
    Try
        If tempListBox.CheckedItems.Count > 0 Then
            Dim tempItem As Object
            For Each tempItem In tempListBox.CheckedItems
                m_ChkItemsString = m_ChkItemsString & "," & tempItem.ToString()
            Next
        End If
        m_ChkItemsString = m_ChkItemsString.Trim().Substring(1, m_ChkItemsString.Length - 1)
    Catch ex As Exception

    End Try
    Return m_ChkItemsString
End Function
Public Sub setText(ByVal chklist As CheckedListBox)
    If chklist.Items.Count > 0 Then
        If chklist.CheckedIndices.Count = chklist.Items.Count Then
            txt.Text = SelectAllText
            Exit Sub
        End If
        If chklist.CheckedIndices.Count > 0 Then
            txt.Text = chklist.CheckedIndices.Count & " selected"
        ElseIf chklist.CheckedIndices.Count = 0 Then
            txt.Text = SelectNoneText
        End If
    Else
        txt.Text = SelectNoneText
    End If

End Sub

Private Sub bChkLstBox_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)
    If Not dataList Is Nothing Then
        If dataList.GetUpperBound(0) > 0 Then
            For i As Integer = 0 To dataList.GetUpperBound(0)
                If Not dataList(i) Is Nothing Then
                    Checklisbox.Items.Add(dataList(i))
                End If
            Next
        End If
    End If
End Sub



'Removed mouse events

End Class

此代码有效,但我发现了一个问题。 当我从列表中选择项目1,2和3并告诉它在列表框中显示项目时,我想出了以下内容:1,1,2,1,2,3。 我仍然会仔细阅读代码,试图解决这个问题,但更有经验的个人建议会有所帮助。

提前致谢,并为长代码道歉。

编辑:

将项目发送到列表框

的代码
 Dim Litems As New List(Of String)
    ListBox1.Items.Clear()
    Litems.Clear()
    For Each I As String In DropDownCheckedListBox1.Checklisbox.CheckedItems
        Litems.Add(I)
        ListBox1.Items.AddRange(Litems.ToArray)
  next

1 个答案:

答案 0 :(得分:1)

将您的AddRange行移至循环外部,否则,您不断将列表内容重新添加到ListBox中:

Dim Litems As New List(Of String)
ListBox1.Items.Clear()
Litems.Clear()
For Each I As String In DropDownCheckedListBox1.Checklisbox.CheckedItems
    Litems.Add(I)        
next
ListBox1.Items.AddRange(Litems.ToArray)