Displaying data from a list box to output

RickyUser6

Member
Joined
Jan 27, 2006
Messages
7
Programming Experience
1-3
Hello guys,
Ive been reading the forums and links for the past 2 days, very useful stuff!

Now my question,
I have a listbox that I loaded with a .txt file and I want to click on a selection in the list box and have that selection appear on another list box.

I want to search the .txt file and grab the selection that matches and print the whole line. But im stuck!

I have

Dim strSpecialties As String
strSpecialties = CStr(lstSpecialties.Items(lstSpecialties.SelectedIndex))

So the selection is stored on strSpecialties.

So how do I go and match this selection from the .txt file? I'm very new at this.

Thanks
 
First off, assuming that you have added String objects to the ListBox in the first place then just use the SelectedItem instead of the item at the SelectedIndex:
VB.NET:
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] strSpecialties [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2] = [/SIZE][SIZE=2][COLOR=#0000ff]CStr[/COLOR][/SIZE][SIZE=2](lstSpecialties.SelectedItem)[/SIZE]
If you have added some other type of objects, like if you have bound data to the control, you should use GetItemText to get the displayed string:
VB.NET:
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] strSpecialties [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2] = [/SIZE][SIZE=2][COLOR=#0000ff]CStr[/COLOR][/SIZE][SIZE=2]([/SIZE][SIZE=2]lstSpecialties.GetItemText([/SIZE][SIZE=2]lstSpecialties.[/SIZE][SIZE=2]SelectedItem))[/SIZE]
As for your question, it's not really clear what you mean by "match this selection from the .txt file". What exactly do you want to get from the file? The selected item if it is there? The whole line that contins the selected item? Something else?
 
hey man, thanks for the reply!
I'm sorry for not being clear on the other question. What Im trying to do is to get the whole line that contins the selected item.

For example the .txt files contains
Susan 38RN Healthcare
Mary 30PA DentalCare
and many more entries

What is being displayed on my listbox are Healthcare,DentalCare, etc.

So I want to click on Healthcare and have all the names of people with Healthcare appear in the output.

thx again!
 
Whilst reading in the file, fill a list with all the people per specialty. Put the lists in a hashtable with the specialty as the key and the list as value.
Retrieve the list for all people with certain speciality with the key (name of the specialty).

How do you plan to handle duplicates (I guess peoples names are not unique)? If the numbers 38RN and 30PA are unique per person you could use another HashTable for the list of people with a certain specialty.
 
Given that you must be reading the entire file to get the values for the ListBox in the first place, you should probably maintain all the other data as well. That way you don't have to go back to the text file again. I would use a Hashtable for that. The keys would be "HealthCare", "DentalCare", etc. and the values would be either StringCollections containing all the names that correspond to the key or else ArrayLists of instances of your own type that contain the names and any other data you need to store, like that four-character alphanumeric code. Once that's done getting the data is a piece of cake. When the user selects an item in the ListBox you simply use that value to index the Hashtable and it gives you back a collection that contains all the data you need.
 
Back
Top