在列表框中选择下一个项目

本文关键字:下一个 项目 选择 列表 | 更新日期: 2023-09-27 18:14:34

我试图循环通过一个列表框,选择下一个项目,每次循环。但是当我运行应用程序时,它不会转到下一个项目,它只是使用第一个选中的项目。

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;
    } 
}

编辑:删除转换。ToInt32

Edit 2:编辑代码以反映更改

编辑3:正确代码

在列表框中选择下一个项目

你的逻辑错了。你需要在循环内修改URL,而不是在它上面:

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;
}

你可以这样做:

上一项:

if (lb.SelectedIndex > 0)
{ 
 lb.SelectedIndex = lb.SelectedIndex - 1; 
}

下一个项目:

if (lb.SelectedIndex < lb.Items.Count - 1)
{
 lb.SelectedIndex = lb.SelectedIndex + 1;
} 

如果您需要详细信息,可以参考:

http://social.msdn.microsoft.com/forums/vstudio/en - us/92fdb8e2 - 47 - c9 - 49 - a0 - 8063 - 8533 - b78f41d0/c -列表框选择nextprevious?forum=csharpgeneral

希望有帮助!