Alenhj
Alenhj

Reputation: 109

DropDownCheckedListBox Issue

Was looking at making a DropDownCheckedListBox for one of my forms. I found a complete source code that was on line.

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

This code works but I discovered a problem. When I select items 1, 2, and 3 from the list and tell it to display the items in an list box I come up with the following: 1, 1, 2, 1, 2, 3. I am still going over the code trying to figure it out but more experience individuals advice would be helpful.

Thanks in advance, and Apologies for the long code.

Edit:

Code to send items to 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)
        ListBox1.Items.AddRange(Litems.ToArray)
  next

Upvotes: 1

Views: 107

Answers (1)

LarsTech
LarsTech

Reputation: 81635

Move your AddRange line to outside the loop, otherwise, you keep re-adding the contents of your list to the 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)

Upvotes: 1

Related Questions