A Listbox can be useful for displaying data when you have multiple items/records to display. You might want to add an item to a listbox, display items in a listbox, loop through each item in a listbox, and output contents from a listbox to another place, e.g. a textfile or textboxes on the form
When the OK button is pressed the code will take the value entered in the textbox txtEnterMark and display it in the listbox lstExamMarks
Here is the code:
...from here to here... ->
find this code in my drive, VB file handling solution - formC
Select an item in a listbox and display the details in separate textboxes:
When you have several items in a listbox, it can be useful to loop through each item in the listbox, from start to finish, adding up values, or maybe writing each line in the listbox to a textfile.
Pseudocode:
For Count = 0 to ListboxName.Items.Count - 1
‘put your writing to textfile, or anything else you want to do with each item in the listbox, here …..
Next Count
Notes:
Count will go from 0, as the first row in your listbox is position 0 (not pos 1)
Change ListboxName to the name of your listbox
The -1 at the end of the For statement is because you started at zero, so if there are 10 rows in your listbox, you need to loop from 0 to 9
You only need to write your code once, in the middle of the loop (highlighted text). It will then run however many times you have told it to i.e. 10 times in my example above.
In this example, products are added to a basket (listbox). When the 'Place Order' button is pressed, the Username (from login) and each Product Code (from the listbox) are written out to an Orders textfile.
Note: Three products in the basket
Code written behind 'Place Order' button, which saves the Username and Product Code to Orders.txt
Here is the Orders textfile showing the three items that were in the basket.
Note: When creating an order, it is always useful to store the date the order was placed too.
To remove an item from a list box - when you know what the value/text is:
To remove the last item from a list box:
To remove a selected item from a list box: