【问题标题】:Selecting both listbox item by just selecting on one listbox item from another listbox通过从另一个列表框中选择一个列表框项目来选择两个列表框项目
【发布时间】:2015-10-12 12:10:23
【问题描述】:

首先我要从ListBox1 中选择一个项目,然后如果我在ListBox1 中选择一个项目,那么ListBox2 的相应索引也应该被选择。

我的ListBox1代码

private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    listBox1.SelectedIndex = listBox2.SelectedIndex;
}

这里是ListBox2

private void listBox2_SelectedIndexChanged(object sender, EventArgs e)
{
    listBox1.SelectedIndex = listBox2.SelectedIndex;
}

这个方法我没看清楚,肯定是搞错了。

我只是在这部分需要帮助,希望你们能分享一些这方面的知识。

【问题讨论】:

    标签: c# listbox


    【解决方案1】:

    像这样更改您的代码:

    private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
         if (listBox2.Items.Count >= listBox1.SelectedIndex + 1)
         {
              listBox2.SelectedIndex = listBox1.SelectedIndex;
         }
    }
    
    private void listBox2_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (listBox1.Items.Count >= listBox2.SelectedIndex + 1)
        {
             listBox1.SelectedIndex = listBox2.SelectedIndex;               
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-28
      • 2023-03-04
      • 2021-11-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多