Link to home
Start Free TrialLog in
Avatar of cgaDesign
cgaDesign

asked on

VB.NET Winforms Multiselect Listbox Traverse ?

In vb.net 2005, winforms, how do you traverse a multiselect Listbox ? I need to extract the selected values, and build a SQL Select statement.  I can write the loop, but Listbox.Selected() is not recognized (must be for Webforms). Thank you.
Avatar of cdaly33
cdaly33
Flag of United States of America image

Avatar of cgaDesign
cgaDesign

ASKER

No ... that example refers to Classic ASP ... I am using VB.NET WinForms.

    Dim strWhere As String = "WHERE sClass IN("
 
 
    Dim i As Integer = 0
    Dim str As String = ""
    For i = 0 To ListBox1.SelectedItems.Count - 1
      strWhere &= "'" & ListBox1.SelectedItems(i).ToString() & "',"
    Next
 
 
    strWhere = strWhere.TrimEnd(",") & ")"

Open in new window

The value of my strWhereUnit variable becomes, during output:
     Or P.intUnitFK = 'System.Data.DataRowView'
for each Selected Item. I am supposed to get integers as values.

Dim strWhere As String = ""
 For i = 0 To Me.lstLkUnit.SelectedItems.Count - 1
    strWhereUnit += " Or P.intUnitFK = '" + Me.lstLkUnit.SelectedItems(i).ToString() + "'"
 Next i

Thank you
ASKER CERTIFIED SOLUTION
Avatar of cdaly33
cdaly33
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
OK, that worked.  Guess I need to look up CType, and bound Listboxes. My Listbox is actually bound to a bindingsource object.  This probably should have been worth more points ...
Thanks much !

The essence was that I needed to realize that I was traversing a DATABOUND Listbox.
The essence to the problem was that I needed to realize that I was trying to traverse a DATABOUND Winforms Listbox.