Reputation: 67
I'm trying to loop through a listbox, selecting the next item every loop. But whenever I run the application it doesn't go to the next item, it just uses the first selected item.
lb.SelectedIndex = 0;
for (int i = 0; i < lb.Items.Count; i++)
{
using (var process = new Process())
{
string tn = lb.SelectedItem.ToString();
string url = "https://enterprisecenter.verizon.com/enterprisesolutions/global/dlink/repairs/iRepair/DelegateDispatch.do?exec=delegateRoute&action=VIEW_BY_NUMBER_VIEW_TKT_SECTION&ticketNumber=" + tn + "&state=";
process.StartInfo.FileName = @"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe";
process.StartInfo.Arguments = url;
process.Start();
}
if (lb.SelectedIndex < lb.Items.Count - 1)
{
lb.SelectedIndex = lb.SelectedIndex + 1;
}
}
Edit: Removed Convert.ToInt32
Edit 2: Edited code to reflect changes
Edit 3: Correct Code
Upvotes: 0
Views: 879
Reputation: 125757
Your logic is wrong. You need to modify the URL inside your loop, not above it:
lb.SelectedIndex = 0;
for (int i = 0; i < lb.Items.Count; i++)
{
using (var process = new Process())
{
string tn = lb.SelectedItem;
string url = "privateURL" + tn + "privateURL";
process.StartInfo.FileName = @"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe";
process.StartInfo.Arguments = url;
process.Start();
}
lb.SelectedIndex := lb.SelectedIndex + 1;
}
Upvotes: 2
Reputation: 9454
You could do something like this:
for the previous item:
if (lb.SelectedIndex > 0)
{
lb.SelectedIndex = lb.SelectedIndex - 1;
}
next item:
if (lb.SelectedIndex < lb.Items.Count - 1)
{
lb.SelectedIndex = lb.SelectedIndex + 1;
}
If you need in detail you could refer this:
Hope it helps!
Upvotes: 0