Wrox Programmer Forums
|
ASP.NET 4 General Discussion For ASP.NET 4 discussions not relating to a specific Wrox book
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 4 General Discussion section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 31st, 2010, 03:53 PM
Authorized User
 
Join Date: Jul 2010
Posts: 23
Thanks: 7
Thanked 2 Times in 1 Post
Default ListBox selected items

Hello

I have a ListBox where i list different tags a picture can be associated with. My problem is that i dont know how to check wich rows are selected. I have tried a few things without succes. Here is the code that i have written so far:

Code:
        Using myEnt As New PicturesEntities()
            'Her finner jeg ID'en til det bildet som sist ble satt inn
            Dim picCount = From tag In myEnt.Pictures
                           Order By tag.Id Descending
                           Select tag.Id
            ' For hver tag selected må jeg lage en ny TagLink
            For count As Int16 = 0 To TagLB.Items.Count - 1
                TagLB.SelectedIndex = count
                If TagLB.Items(count).Selected Then '  <-- Here i need a statement that works.
                    Dim tg As New TagLink

                    Dim tagsX = From tag In myEnt.Tags
                                Order By Tag.Id
                                Skip count
                                Select tag.Id

                    tg.TagId = tagsX.First
                    tg.PictureId = picCount.FirstOrDefault + 1
                    myEnt.TagLinks.AddObject(tg)
                    myEnt.SaveChanges()
                End If
            Next
        End Using
All my solutions ended up selecting all the items in the listbox, and not just the selected ones.
 
Old August 31st, 2010, 05:02 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi there,

I haven't really looked at the code and its intention in detail, but I did notic one thing:

TagLB.SelectedIndex = count
If TagLB.Items(count).Selected Then ' <-- Here i need a statement that works.

Look at the line in bold. Aren't you simply making each item the selected item in the loop by setting the SelectedIndex right before you check the Selected property?

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
The Following User Says Thank You to Imar For This Useful Post:
andreas5 (August 31st, 2010)
 
Old August 31st, 2010, 06:03 PM
Authorized User
 
Join Date: Jul 2010
Posts: 23
Thanks: 7
Thanked 2 Times in 1 Post
Default

Yes you are right, that was kind of obvious i guess. However i mostly tried that statement because i could not get anything else to work.

I just found the solution now. I had to set the AppendDataBoundItemsproperty of the ListBox to True, aswell as removing the statement you pointed out.

Thanks for looking through my questions once again.

Edit: Here is another attempt in case someone is interested.
Code:
        
        Using myEnt As New PicturesEntities()
            
            Dim picCount = From tag In myEnt.Pictures
                           Order By tag.Id Descending
                           Select tag.Id

            For Each li As ListItem In TagLB.Items
                If li.Selected Then

                    Dim tg As New TagLink
                    Dim tx As ListItem = li ' (compiler complained when i used the iterator)

                    Dim tagsX = From tag In myEnt.Tags
                                Order By tag.Id
                                Where tag.TagName = tx.Text
                                Select tag.Id

                    tg.TagId = tagsX.First
                    tg.PictureId = picCount.FirstOrDefault + 1
                    myEnt.TagLinks.AddObject(tg)
                    myEnt.SaveChanges()
                End If
            Next
        End Using

Last edited by andreas5; August 31st, 2010 at 06:52 PM..





Similar Threads
Thread Thread Starter Forum Replies Last Post
moving items from one listbox to another listbox Nishapd ASP.NET 3.5 Basics 2 December 3rd, 2009 07:20 AM
multiple Listbox values in another listbox terryv Excel VBA 0 June 27th, 2007 07:01 AM
ListBox kanoorani Beginning VB 6 4 September 12th, 2006 12:28 PM
Listbox vanjamier Pro JSP 0 January 6th, 2005 09:44 PM
I'm back :) Listbox var from listbox MichaelTJ .NET Web Services 2 October 21st, 2003 07:06 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.