How to loop though a listbox with out making the User select all items in the listbox

Published:
What my article will show is if you ever had to do processing to a listbox without being able to just select all the items in it.

My software Visual Studio 2008 crystal report v11

My issue was I wanted to add crystal report to a form and show a report of what the user had added to a listbox.  Without making the user(s) select all of them  in the listbox. Unless you directly select the items in the listbox there no way to go though them that I have found listed on the web.   I found a way to process all items in a listbox without making a user select them.

I have a project where I create a form with 2 listbox, a textbox, a rich text box. We load the first textbox with a list of reasons for our clients to be rejected for an application. Since each client reasons can be different we have a flexible way to keep up to date.

We have a master list we load into listbox1. Each of the reasons has a text description with it. The user selects from listbox1 and it gets added to listbox2. I set it so when the user double clicks on a reason in listbox1 its gets add to listbox2.  The description appears in the richtextbox and can be changed.  We have a crystal report  attached to the form to show the letter we will send.  

The problem I was having was after my user builds listbox2 there is not an easy way to select all the reasons the user added in listbox2. We don’t want to explain that to create the letter the user has to select all the added reasons in listbox2. I have found there is no easy way to select all in a listbox when creating the review report.  

Well I figured out an easy way to loop thought the second listbox and fake it out that you have selected each item. You have listbox.items.count (be careful this is not zero based) to tell you how many items they selected. I then loop though and fake out the selecteditems listbox.SelectedItem property.

 
Private sub loopthoughlistbox
                      Dim x as interger = 0
                      ‘set to count how many items I have in the listbox
                      x = listbox2.Items.Count
                      Dim index, theindex As Integer
                      index = 0
                      ‘this will process in  order since yoru starting with x as highest
                               
                              Do Until x < 1
                        
                                  'remember the count is not zero based 3 items = count of 3 not 2 
                                   As the third item selected is in table position 2
                                 
                                  Dim c As String
                                  Dim d As String
                                  index = x – 1
                      ‘this fake it out and it thinks this item is the selected one
                                  listbox2.SetSelected(index, True)
                      			
                                  b = CType(listbox2.SelectedItem, clsCategoryListItem).itemID
                                  ‘do sub to load whatever
                      		‘Set x down by one 
                                  x = x - 1
                      
                                 Loop
                      
                      End sub
                      
                      
                      Put in order entered 
                      ‘replace with this if  you want to do it in ordered entered
                      
                      Dim x as interger = 1
                       
                      Dim index, theindex As Integer
                      
                              Do Until x > listbox2.Items.Count
                        
                                  'remember the count is not zero based 3 items = count of 3 not 2 
                                   As the third item selected is in table position 2
                                 
                                  Dim c As String
                                  Dim d As String
                                  index = x – 1
                      ‘this fakes it out and it thinks this item is the selected one
                                  listbox2.SetSelected(index, True)
                      			
                                  b = CType(listbox2.SelectedItem, clsCategoryListItem).itemID
                                  ‘do sub to load whatever
                      		‘Set x up by 1
                                  x = x + 1
                      
                                 Loop
                      
                      
                      
                       Done forget you will go though selectedIndexChanged sub if you have one each time you  change index above
                      
                           Private Sub listbox2selected(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles listbox2.SelectedIndexChanged
                      ‘ you always need to check  if somehting  is in the listbox
                              If Not IsNothing(listbox2.SelectedItem) Then
                      	‘Do whatever
                              End If
                          End Sub
                      
                      There are plenty of examples showing you how add to a listbox.

Open in new window

0
3,460 Views

Comments (0)

Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.