Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

VB.NET Winforms Multiselect Listbox Traverse ?

Posted on 2009-03-31
8
Medium Priority
?
949 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

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

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!

Question has a verified solution.

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

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …

963 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