Solved

VB.NET Winforms Multiselect Listbox Traverse ?

Posted on 2009-03-31
8
925 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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
 
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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

710 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