locked
Add Checked Item to Listbox RRS feed

  • Question

  • Firstly, I'm begginer and please, be patient with me.

    I believe that this is a simple question, but I can't do.
    I need that when click in ">>" the "Objeto" Checked (left side) be added in ListBox (right side) and be deleted of CheckListBox

    Any sugestion?
    Thx.
    Monday, December 22, 2008 11:33 PM

Answers

  • Hi Reed, One more option

      While CheckedListBox1.CheckedItems.Count > 0
                ListBox1.Items.Add(CheckedListBox1.CheckedItems(0))
                CheckedListBox1.Items.Remove(CheckedListBox1.CheckedItems(0))
            End While

    Arjun Paudel
    Tuesday, December 23, 2008 3:45 PM
  • Hi Lucas,

    Assuming the controls were named Button1, CheckedListBox1, and ListBox1 a little routine like this should do it:

        Private Sub Button1_Click(ByVal sender As System.ObjectByVal e As System.EventArgs) Handles Button1.Click  
            Dim listPosition As Integer = ListBox1.Items.Count  
            For i As Integer = CheckedListBox1.CheckedItems.Count - 1 To 0 Step -1  
                ListBox1.Items.Insert(listPosition, CheckedListBox1.CheckedItems.Item(i))  
                CheckedListBox1.Items.Remove(CheckedListBox1.CheckedItems.Item(i))  
            Next 
        End Sub 

    So the idea is we loop through the checked item's collection on the CheckedListBox and add each one to the ListBox items collection, then remove it from the CheckedListBox items collection.

    But we have to do this a certain way because we are modifing the collection that we are looping through.  Each time we remove an item from the CheckedListBox CheckedItems collection, we change that collection.  We can't use a For-Each loop or any other Enumerator because an Enumerator will throw an exception (cause an error) if you try to modify the collection it is currently enumerating.  So we have to use For I = Start to Finish, but even that has to be modified to run in reverse:  For I = Finish to Start Go Backward One At a Time.  If we tried to run the loop forward, the collection would be shrinking as the index was increasing and we'd eventually run into a problem.

    So we loop backward through the list of checked items.  Because we are going backward, we have to take an extra step when inserting into the ListBox Items collection, otherwise we would insert the items backward too.  So first we store the count of items - this gives us the current end of the list.  Now each item from the checked list box is inserted into the listbox at that position.  This keeps the items in the same order between list boxes.

    Ok, that's my expanded answer on a simple question for the benefit of the beginner! hahaha

    Hope that helps!
    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"
    Tuesday, December 23, 2008 4:05 AM
    Moderator
  • Thank very much. I used this code and solved my problem...

     'Create a "clone" of items list selected
     
     'remove the items of original list
     
     Dim selectedItems As New ArrayList(Me.myCheckedListBox.CheckedItems) 
     
      
     
     Dim item As Object 
     
      
     

     
     For Each item In selectedItems
     
     
    'Add each item in ListBox
     
    Me.myListBox.Items.Add(item) 
     
      
     
    'And remove eacht item of CheckedListBox
     
    Me.myCheckedListBox.Items.Remove(item) 
     
     Next 

    Again, thank your very much.
    Tuesday, December 23, 2008 4:50 PM

All replies

  • Hi Lucas,

    Assuming the controls were named Button1, CheckedListBox1, and ListBox1 a little routine like this should do it:

        Private Sub Button1_Click(ByVal sender As System.ObjectByVal e As System.EventArgs) Handles Button1.Click  
            Dim listPosition As Integer = ListBox1.Items.Count  
            For i As Integer = CheckedListBox1.CheckedItems.Count - 1 To 0 Step -1  
                ListBox1.Items.Insert(listPosition, CheckedListBox1.CheckedItems.Item(i))  
                CheckedListBox1.Items.Remove(CheckedListBox1.CheckedItems.Item(i))  
            Next 
        End Sub 

    So the idea is we loop through the checked item's collection on the CheckedListBox and add each one to the ListBox items collection, then remove it from the CheckedListBox items collection.

    But we have to do this a certain way because we are modifing the collection that we are looping through.  Each time we remove an item from the CheckedListBox CheckedItems collection, we change that collection.  We can't use a For-Each loop or any other Enumerator because an Enumerator will throw an exception (cause an error) if you try to modify the collection it is currently enumerating.  So we have to use For I = Start to Finish, but even that has to be modified to run in reverse:  For I = Finish to Start Go Backward One At a Time.  If we tried to run the loop forward, the collection would be shrinking as the index was increasing and we'd eventually run into a problem.

    So we loop backward through the list of checked items.  Because we are going backward, we have to take an extra step when inserting into the ListBox Items collection, otherwise we would insert the items backward too.  So first we store the count of items - this gives us the current end of the list.  Now each item from the checked list box is inserted into the listbox at that position.  This keeps the items in the same order between list boxes.

    Ok, that's my expanded answer on a simple question for the benefit of the beginner! hahaha

    Hope that helps!
    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"
    Tuesday, December 23, 2008 4:05 AM
    Moderator
  • Hi Reed, One more option

      While CheckedListBox1.CheckedItems.Count > 0
                ListBox1.Items.Add(CheckedListBox1.CheckedItems(0))
                CheckedListBox1.Items.Remove(CheckedListBox1.CheckedItems(0))
            End While

    Arjun Paudel
    Tuesday, December 23, 2008 3:45 PM
  • Hi Arjun,

    That is probably the simplest solution - but I tend to teach as I've taught...  Learn the hard way first so that you understand the complexities involved.  Once you've grasped what's going on, then you can learn the "short cuts" that make life easier.  =)

    There are probably a hundred other ways to achieve this final outcome as well;  e.g.

        Private Sub Button1_Click(ByVal sender As System.ObjectByVal e As System.EventArgs) Handles Button1.Click  
            Dim chkd() As Object = CheckedListBox1.CheckedItems.Cast(Of Object).ToArray  
            ListBox1.Items.AddRange(chkd)  
     
            Dim diff() As Object = CheckedListBox1.Items.Cast(Of Object).Except(chkd).ToArray  
            CheckedListBox1.Items.Clear()  
            CheckedListBox1.Items.AddRange(diff)  
        End Sub 

    That routine is probably wasteful from a resources standpoint, but the result is the same. 

    In any event, the code you posted is ultimately what the OP should probably be doing.  Thanks for the input!
    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"
    Tuesday, December 23, 2008 4:21 PM
    Moderator
  • Thank very much. I used this code and solved my problem...

     'Create a "clone" of items list selected
     
     'remove the items of original list
     
     Dim selectedItems As New ArrayList(Me.myCheckedListBox.CheckedItems) 
     
      
     
     Dim item As Object 
     
      
     

     
     For Each item In selectedItems
     
     
    'Add each item in ListBox
     
    Me.myListBox.Items.Add(item) 
     
      
     
    'And remove eacht item of CheckedListBox
     
    Me.myCheckedListBox.Items.Remove(item) 
     
     Next 

    Again, thank your very much.
    Tuesday, December 23, 2008 4:50 PM