<

[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x

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

Published on
8,697 Points
2,697 Views
Last Modified:
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
Comment
Author:john M
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
0 Comments

Featured Post

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Join & Write a Comment

This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…

Keep in touch with Experts Exchange

Tech news and trends delivered to your inbox every month