Results 1 to 7 of 7

Thread: Accessing Multiple Items in a Listbox

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Nov 2012
    Posts
    158

    Accessing Multiple Items in a Listbox

    I've been trying to think about the best way to do this..Hopefully someone here can give me some advice/input!

    I have a listbox and the items in it will vary. Sometimes it might have 300 items, it could have 406, 1029..any number of items basically.
    I have an API I'm using which I'm using and will pass each item in the listbox through. This API will let me take and pass upto 50 items at once..

    Now, I'm assuming the best way to go about doing this would be to throw these items into an array and then access them 50 at a time..However, I could see this posing a problem if there are 303 items in there because after the 6th run there will only be 3 items left.

    What would be the best way to handle this? I want to pass up to 50 items in each request (these items have to be placed into the API http request I'm making), but each request might not actually have 50 items in it...

    Thanks in advance!

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: Accessing Multiple Items in a Listbox

    try this:

    Code:
    Public Class Form1
    
        Dim startAt As Integer = 0
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim take As Integer = Math.Min(50, ListBox1.Items.Count - startAt)
            Dim items() As String = Enumerable.Range(0, take).Select(Function(x) ListBox1.GetItemText(ListBox1.Items(startAt + x))).ToArray
            startAt += 50
             
            'Stop
        End Sub
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            'this is just for testing purposes
            For x As Integer = 1 To 303
                ListBox1.Items.Add("item" & x.ToString)
            Next
        End Sub
    
    End Class

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    Re: Accessing Multiple Items in a Listbox

    You can throw a bit of LINQ at the problem and do something like this:
    vb.net Code:
    1. For i = 0 To Integer.MaxValue
    2.     Dim page = Me.ListBox1.Items.Cast(Of Object).Skip(i * 50).Take(50).ToArray()
    3.  
    4.     If page.Length = 0 Then
    5.         Exit For
    6.     End If
    7.  
    8.     'Use page here.
    9. Next
    That will repeatedly give you an Object array with up to 50 elements. Skip and Take automatically handle situations where the number of items is less than the number specified so you don't even have to think about it. You can obviously adjust that as necessary, e.g. cast as type String instead of Object.

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Nov 2012
    Posts
    158

    Re: Accessing Multiple Items in a Listbox

    Thanks for the responses guys! I didn't get a chance to try yours paul, but jmc's idea worked like a charm! One other issue I'm running into now though..

    Now that I have the array built (using strings instead of objects)..I want to put them together, and add some standard text before each one. To do that, I'm using string.join..Here is the code I've been testing with

    Code:
    Dim JoinDomains As String = String.Join("&domain" & i & "=", page)
    What I'm left with is:
    domain1.com&domain0=domain2.com&domain0=domain3.com&domain0=domain4.com..etc.

    When the correct format should be:
    &domain0=domain1.com&domain1=domain2.com&domain2=domain3.com&domain3=domain4.com

    Obviously the "i" is misplaced in my code because I'm doing that inside the loop you created in your original response, that makes sense as to why I'm getting "domain0" everytime..But, what's the best way to go about handling this? Make another loop that now loops through "page" and creates the integer for "domain"?

    Thanks!!

  5. #5
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: Accessing Multiple Items in a Listbox

    try this:

    Code:
    Dim domains() As String = {"domain1.com", "domain2.com", "domain3.com"}
    Dim s As String = String.Concat(Enumerable.Range(0, domains.Count).Select(Function(x) "&domain" & x.ToString & "=" & domains(x)).ToArray)

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    Re: Accessing Multiple Items in a Listbox

    Quote Originally Posted by digitaldrew View Post
    Thanks for the responses guys! I didn't get a chance to try yours paul, but jmc's idea worked like a charm! One other issue I'm running into now though..

    Now that I have the array built (using strings instead of objects)..I want to put them together, and add some standard text before each one. To do that, I'm using string.join..Here is the code I've been testing with

    Code:
    Dim JoinDomains As String = String.Join("&domain" & i & "=", page)
    What I'm left with is:
    domain1.com&domain0=domain2.com&domain0=domain3.com&domain0=domain4.com..etc.

    When the correct format should be:
    &domain0=domain1.com&domain1=domain2.com&domain2=domain3.com&domain3=domain4.com

    Obviously the "i" is misplaced in my code because I'm doing that inside the loop you created in your original response, that makes sense as to why I'm getting "domain0" everytime..But, what's the best way to go about handling this? Make another loop that now loops through "page" and creates the integer for "domain"?

    Thanks!!
    If I'm reading that correctly then this demonstrates what you want:
    vb.net Code:
    1. Module Module1
    2.  
    3.     Sub Main()
    4.         Dim domains = Enumerable.Range(0, 50).Select(Function(n) "domain" & n & ".com").ToArray()
    5.         Dim str = String.Concat(Enumerable.Range(0, domains.Length - 1).Select(Function(i) String.Format("&{0}={1}", domains(i), domains(i + 1))))
    6.  
    7.         Console.WriteLine(str)
    8.         Console.ReadLine()
    9.     End Sub
    10.  
    11. End Module
    That raises a couple of questions though:

    1. What to do if you end up with just a single item in the last page?
    2. Is it OK that the last item in one page and the first item in the next page never end up in a comparison with each other when every other adjacent pair do?

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Nov 2012
    Posts
    158

    Re: Accessing Multiple Items in a Listbox

    Thanks for the replies both of you!! This time I gave paul's code a shot and it seamed to do the trick! I now have it working and sending everything just fine! You guys have been a huge help..I managed to get everything I needed with just a few short lines of code.. This would have taken me a while to figure out so thanks again!!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width