-
Aug 29th, 2014, 01:01 AM
#1
Thread Starter
Addicted Member
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!
-
Aug 29th, 2014, 01:31 AM
#2
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
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Aug 29th, 2014, 01:49 AM
#3
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:
For i = 0 To Integer.MaxValue Dim page = Me.ListBox1.Items.Cast(Of Object).Skip(i * 50).Take(50).ToArray() If page.Length = 0 Then Exit For End If 'Use page here. 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.
-
Aug 29th, 2014, 02:51 AM
#4
Thread Starter
Addicted Member
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!!
-
Aug 29th, 2014, 03:12 AM
#5
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)
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Aug 29th, 2014, 03:19 AM
#6
Re: Accessing Multiple Items in a Listbox
Originally Posted by digitaldrew
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:
Module Module1 Sub Main() Dim domains = Enumerable.Range(0, 50).Select(Function(n) "domain" & n & ".com").ToArray() Dim str = String.Concat(Enumerable.Range(0, domains.Length - 1).Select(Function(i) String.Format("&{0}={1}", domains(i), domains(i + 1)))) Console.WriteLine(str) Console.ReadLine() End Sub 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?
-
Aug 29th, 2014, 05:30 AM
#7
Thread Starter
Addicted Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|