首页 文章

选择列表框C#中的下一个项目

提问于
浏览
0

我正在尝试遍历列表框,每个循环选择下一个项目 . 但每当我运行应用程序时,它都不会转到下一个项目,它只使用第一个选定的项目 .

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

编辑:删除Convert.ToInt32

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

编辑3:正确的代码

2 回答

  • 0

    你的逻辑错了 . 您需要修改循环内的URL,而不是它上面的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;
    }
    
  • 2

    你可以这样做:

    对于上一个项目:

    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-47c9-49a0-8063-8533b78f41d0/c-listbox-select-nextprevious?forum=csharpgeneral

    希望能帮助到你!

相关问题