Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. items added in listbox sorted asc[modified]

items added in listbox sorted asc[modified]

Scheduled Pinned Locked Moved ASP.NET
7 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    bubberz
    wrote on last edited by
    #1

    Hello! I used this code, and it works to not allow duplicates: Dim li As New ListItem For Each li In LB1.Items If li.Selected = True Then If Not ListBox2.Items.Contains(li) Then ListBox2.Items.Add(li) End If End If Next After the items are added, my second goal is to sort them ASC too. Haven't quite figured that out as well. Any suggestions are appreciated. Thanks! -- modified at 9:57 Monday 7th August, 2006

    N G 2 Replies Last reply
    0
    • B bubberz

      Hello! I used this code, and it works to not allow duplicates: Dim li As New ListItem For Each li In LB1.Items If li.Selected = True Then If Not ListBox2.Items.Contains(li) Then ListBox2.Items.Add(li) End If End If Next After the items are added, my second goal is to sort them ASC too. Haven't quite figured that out as well. Any suggestions are appreciated. Thanks! -- modified at 9:57 Monday 7th August, 2006

      N Offline
      N Offline
      Not Active
      wrote on last edited by
      #2

      I'm not exactly sure what it is you are trying to do here however to avoid duplicates you could insert the items into a hashtable and bind the listbox to it.

      only two letters away from being an asset

      B 1 Reply Last reply
      0
      • N Not Active

        I'm not exactly sure what it is you are trying to do here however to avoid duplicates you could insert the items into a hashtable and bind the listbox to it.

        only two letters away from being an asset

        B Offline
        B Offline
        bubberz
        wrote on last edited by
        #3

        Thanks Mark! I got the duplicate code to work...I actually put the code in the wrong listbox. So, I'll try the hashtable with the listbox items then for the sort.

        1 Reply Last reply
        0
        • B bubberz

          Hello! I used this code, and it works to not allow duplicates: Dim li As New ListItem For Each li In LB1.Items If li.Selected = True Then If Not ListBox2.Items.Contains(li) Then ListBox2.Items.Add(li) End If End If Next After the items are added, my second goal is to sort them ASC too. Haven't quite figured that out as well. Any suggestions are appreciated. Thanks! -- modified at 9:57 Monday 7th August, 2006

          G Offline
          G Offline
          Guffa
          wrote on last edited by
          #4

          Where do you get the items from? If you for an example get them from a database, it's easier and more efficient to remove duplicates and sort in the database query, than writing code for it.

          --- b { font-weight: normal; }

          B 1 Reply Last reply
          0
          • G Guffa

            Where do you get the items from? If you for an example get them from a database, it's easier and more efficient to remove duplicates and sort in the database query, than writing code for it.

            --- b { font-weight: normal; }

            B Offline
            B Offline
            bubberz
            wrote on last edited by
            #5

            Guffa, I get the items from a db call to populate the left listbox on my page. The user will click on this listbox to transfer the "number" over to the right side listbox, Listbox2, on the page. I'm trying this code to sort the items in Listbox2, but get: At least one object must implement IComparable. Dim items As New ArrayList(ListBox2.Items) items.Sort() ListBox2.DataSource = items

            G 1 Reply Last reply
            0
            • B bubberz

              Guffa, I get the items from a db call to populate the left listbox on my page. The user will click on this listbox to transfer the "number" over to the right side listbox, Listbox2, on the page. I'm trying this code to sort the items in Listbox2, but get: At least one object must implement IComparable. Dim items As New ArrayList(ListBox2.Items) items.Sort() ListBox2.DataSource = items

              G Offline
              G Offline
              Guffa
              wrote on last edited by
              #6

              Implement an IComparer for ListItem, and use that in the call to the Sort method. But, shouldn't you do this using client script instead? If you use server code to move the items, you will be sending a lot of data back and forth between the server and the client.

              --- b { font-weight: normal; }

              B 1 Reply Last reply
              0
              • G Guffa

                Implement an IComparer for ListItem, and use that in the call to the Sort method. But, shouldn't you do this using client script instead? If you use server code to move the items, you will be sending a lot of data back and forth between the server and the client.

                --- b { font-weight: normal; }

                B Offline
                B Offline
                bubberz
                wrote on last edited by
                #7

                Got it to work with this: http://www.411asp.net/func/content?tree=411asp/tutorial/specific/web/userinte/webcontr/listbox&id=5881410 Thanks to all for the help!

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups