Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Retreiving multiple strings from a listbox 1

Status
Not open for further replies.

Mesk69

Programmer
Feb 6, 2003
12
0
0
CA
Hi guys, I'm having a simple problem, I can't beleive I'm stuck trying to get this work, but I can't retreive information from a list box. I can't seen to be getting a string from it. This is the code I use. Any help would be greatly appreciated. Thanks for taking the time to read.

Mesk

For Each course_code In lbCourse.SelectedItems
course_code = course_code.Substring(1, 2) + course_code.Substring(4, 3) + course_code.Substring(7, 2)
'need to finish the SQL statement
StrSQL = "INSERT INTO PROGRAM_COURSE VALUES(PROGRAM_COURSE_SEQ.NEXTVAL, '" + program_code + "', '" + option_code + "', '" + course_code + "', " & academic_year & ", " & year_in_program & ", '" + semester_code + "'"
Next
 
See ListIem Class. There are Text and Value properties.

Try
Dim course_codeItem as lisItem
For Each course_codeItem In lbCourse.SelectedItems
course_code = course_codeItem.Text
course_code = course_code.Substring(1, 2) + Forms/Controls Resizing/Tabbing Control
Compare Code (Text)
Generate Sort Class in VB or VBScript
 
I can't even seem to find the ItemList Class. How do I intergrate it into my project? I tried looking it up and found it in the help but I can't seem to implement it. ListItem doesn't come up.

Thanks for taking the time to read and reply.

Mesk
 
Sorry I spoke too soon. I found out how to make it possible to use ListItem. Now however, I have a new problem. This line:

For Each course_codeItem In lbCourse.SelectedItems

Gives me this exception:

An unhandled exception of type 'System.InvalidCastException' occurred in ProgramCourse.exe

Additional information: Specified cast is not valid.


Any reasons why they aren't 'compatible'. Thanks for taking the time to read and reply.

Mesk
 
I'm having a very similar problem.. I try and get all the selected values from a multi select list box but I seem to be getting an exception. I found out how to do this on a website somewhere but it just doesn't seem to work for me. This is my code..


Dim strName as String
Dim strArr(listNames.SelectedItems.Count - 1) as String
Dim i as Integer

For Each strName In listNames.SelectedItems
strArr(i) = strName
++i
Next

When it reaches the For Each line, it gives me this exception:

"An unhandled exception of type 'System.InvalidCastException' occurred in microsoft.visualbasic.dll

Additional information: Cast from type 'DataRowView' to type 'String' is not valid."

I would really appreciate the help. Thanks for listening!

Samy
 
My mistake. The ListItem Class is for a Web Control. The Listbox now accepts ANY object, not just strings. The
SelectedItems property returns a ListBox.SelectedObjectCollection. I have not waded through it all. I use the GetSelected method of the ListBox which returns a boolean.

For I = 0 To ListBox1.Items.Count - 1
if ListBox1.getSelected(I) = True then
.........
Next Forms/Controls Resizing/Tabbing Control
Compare Code (Text)
Generate Sort Class in VB or VBScript
 
Thanks for your help so far John. However, my main problem is getting the value of the ListBox item. I can't seem to find a method which returns the text. Using that for loop, I can determine which list items are selected but I still don't know how to get their value. The closest I got to getting it was with ListBox1.GetItemText(i) but that returned the index of the selected item.. which is what 'i' is anyway.

Thanks again.

Mesk




 
Try ToString.
Dim course_codeItem as Object
For Each course_codeItem In lbCourse.SelectedItems
course_code = course_codeItem.ToString
course_code = course_code.Substring(1, 2) + Forms/Controls Resizing/Tabbing Control
Compare Code (Text)
Generate Sort Class in VB or VBScript
 
I tried something like that before, but tried it again just to make sure and the string that it returns is "System.Data.DataRowView".

All I would really need is the code you gave me earlier:

For I = 0 To ListBox1.Items.Count - 1
if ListBox1.getSelected(I) = True then
.........
Next

With the code to retreive the value of the selected ListBox1(i) (more than one can be selected) intead of the ".......". In my case, the list box is filled with course names (strings). Any ideas? Thanks again, I really appreciate your help.

Mesk
 
It appears that you have stored System.Data.DataRowView objects in the ListBox. What happens on the following?
Dim course_codeItem as System.Data.DatarowView
For Each course_codeItem In lbCourse.SelectedItems
course_code = course_codeItem.Item course_code = course_code.Substring(1, 2) + Forms/Controls Resizing/Tabbing Control
Compare Code (Text)
Generate Sort Class in VB or VBScript
 
Thanks John. With what you gave me, I was able to get it to work:

Dim course_codeItem As System.Data.DataRowView

For Each course_codeItem In lbCourse.SelectedItems
course_code = System.Convert.ToString(course_codeItem.Item(0)))

Thanks again for your time and efforts.

Mesk
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top