Solved

VB.NET Winforms Multiselect Listbox Traverse ?

Posted on 2009-03-31
8
901 Views
Last Modified: 2012-05-06
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.
0
Comment
Question by:cgaDesign
  • 5
  • 3
8 Comments
 
LVL 9

Expert Comment

by:cdaly33
ID: 24035708
0
 

Author Comment

by:cgaDesign
ID: 24035734
No ... that example refers to Classic ASP ... I am using VB.NET WinForms.
0
 
LVL 9

Expert Comment

by:cdaly33
ID: 24035802

    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

0
 

Author Comment

by:cgaDesign
ID: 24035848
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
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 9

Accepted Solution

by:
cdaly33 earned 200 total points
ID: 24035901
Oh you've databound it.  Try this where DataColumnName is the name of the column that contains that value that you want.
Dim strWhere As String = ""

 For i = 0 To Me.lstLkUnit.SelectedItems.Count - 1

    strWhereUnit += " Or P.intUnitFK = '" + CType(Me.lstLkUnit.SelectedItems(i), DataRowView).Item("DataColumnName").ToString() + "'"

 Next i

Open in new window

0
 

Author Comment

by:cgaDesign
ID: 24035970
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 !

0
 

Author Closing Comment

by:cgaDesign
ID: 31565150
The essence was that I needed to realize that I was traversing a DATABOUND Listbox.
0
 

Author Comment

by:cgaDesign
ID: 24035998
The essence to the problem was that I needed to realize that I was trying to traverse a DATABOUND Winforms Listbox.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now