Solved

VB.NET Winforms Multiselect Listbox Traverse ?

Posted on 2009-03-31
8
889 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

746 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

12 Experts available now in Live!

Get 1:1 Help Now