Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 4 > ASP.NET 4 General Discussion
Password Reminder
Register
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
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.
Reply With Quote
  #2 (permalink)  
Old August 31st, 2010, 05:02 PM
Imar's Avatar
Wrox Author
Points: 72,073, Level: 100
Points: 72,073, Level: 100 Points: 72,073, Level: 100 Points: 72,073, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,587 Times in 1,563 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!
Reply With Quote
The Following User Says Thank You to Imar For This Useful Post:
andreas5 (August 31st, 2010)
  #3 (permalink)  
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..
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 11:25 AM.


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