I have two questions here. First of all, how do I enable selection of multiple listbox items and select them via code?
Secondly, how do I prevent the user from selecting multiple lines?

Now, for some background and my thoughts. I'm working on a project for computer science in which 'movies' (a class module I created) may be borrowed, browsed or requested. Currently, I have it to the point where a user may register or login and their information is saved to a file. I also have the menu after the login screen complete and they can view each and every movie and make requests/borrow them by outputting the information into listboxes.

Each request/borrowed movie is stored into a multi-dimensional array which contains the date and movie's index (I declared an array of movies). Essentially, I want to output the multiple pieces of data for each request as one single listbox item which may be selected and removed from the list as necessary. After a bit of googling and dabbling, I discovered much to my dismay that visual basic 6 list boxes --my CS class uses outdated software >.<-- do not support displaying one list object across multiple lines. As such, I have resorted to the following:

Call lstRequests.AddItem(movStock(requested(0, i)).title)
Call lstRequests.AddItem(requested(1, i))
Call lstRequests.AddItem("")

In order to remove a request, I simply need to use modulo 3 (with some minor modifications) on the listIndex (selected item). This is fine and all, however, I don't want the following to happen (bold is the selection):

"blank string"

What I would like to happen is that the entire entry is automatically and instantaneously selected when the user selects one line. As soon as this selection is complete, I would like the list box to return to single line selection so that the user cannot make multiple selections however, I also want the multi-line selection to remain visible. Namely, I must know how to toggle multi-line selections and also how to choose them using code (please, no VB.NET because my CS class uses outdated VB 6).

Thank you very much for taking the time to read this, and hopefully you can help me surmount this obstacle. Besides this, I am absolutely certain I can figure the rest out considering that I've already surpassed the courses expectations by dealing with files and classes. Once again, thank you for your time, as I know this is a relatively trivial distraction relating to an outdated language.

Recommended Answers

All 3 Replies

To prevent multiple items from being selected you set

ListBox1.SelectionMode = SelectionMode.One

To allow multipl selections use

ListBox1.SelectionMode = SelectionMode.MultiExtended

To select via code you add items to the SelectedItems collection as in


where i is the zero-relative index of the item you want to select. However, once you go back to Single select, all of the lines that are selected except for one will be un-highlighted.

Alright, it seems that leaving it on multi-select is the best option then. Whenever the user makes a change, I'll get the index they selected, clear all selections and multi-select that entry. I'd assume that SelectedItems.clear is a valid command in vb 6, but I'll find out when I get to class tomorrow. Thanks for the help, and thank you very much for mentioning that I can't just explicitly prevent user multi-selection and retain the ability to highlight multiple items. It'll definitely make the project finish a bit more smoothly.

From here, I'll probably write a function which allows highlighted requests to be removed. Then, I need to cannibalize my new user write to file code so that I can load and save requests for each user. Once I have that finished, if I really want to overdo the project, I can throw in a few sorting algorithms and allow the creation of new movie objects. It's kinda sad that they don't expect me to be playing with files and complex datatypes. If anything, the curriculum seems to believe that I should be toying with a huge heap of variabled whose only relation is sharing the title of the movie. Arrays are pretty much as far as I'm expected to go... Using a whole lot of variables is good and all, but I love to focus on taking the tedium out of my programming by having functions do all of it for me.

I'd assume that SelectedItems.clear is a valid command in vb 6

Keep in mind that you asked this question in the vb.net forum, not the vb-4-5-6 forum so I answered based on that.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.