简体   繁体   English

Select / 根据在一个列表框中选择一个项目取消选择多个列表框中的项目 - C# Windows forms

[英]Select / Deselect Items in multiple listboxes based on selection of an item in one listbox - C# Windows forms

I have a scenario where I have three listboxes.我有一个场景,我有三个列表框。 Multiple Items can be selected in the listboxes.可以在列表框中选择多个项目。 Based on the selection of items in one listbox, I need to select and deselect the corresponding rows in the other listboxes.基于一个列表框中项目的选择,我需要 select 并取消选择其他列表框中的相应行。

I have the code below, but I am missing something.我有下面的代码,但我遗漏了一些东西。 When I select and deselecting the item, it selects and deselects the other rows items incorrectly.当我 select 并取消选择该项目时,它会错误地选择和取消选择其他行的项目。

            for (int count = 0; count < listBox_1.SelectedIndices.Count; count++)
            {
                // Determine if the item is selected.
                if (listBox_1.GetSelected(count) == true)
                {
                    listBox_2.SetSelected(listBox_1.SelectedIndices[count], false);
                    listBox_3.SetSelected(listBox_1.SelectedIndices[count], false);
                }
                else if (listBox_1.GetSelected(count) == false)
                {
                    // Select all items that are not selected.
                    listBox_2.SetSelected(listBox_1.SelectedIndices[count], true);
                    listBox_3.SetSelected(listBox_1.SelectedIndices[count], true);
                }

            }

Here Selection of items in LB1 should control the selection in LB2 and LB3.此处 LB1 中项目的选择应控制 LB2 和 LB3 中的选择。 Now, since Item 2 and 3 are selected in LB1 - items 2 and 3 should be selected in LB2 and LB3 as well.现在,由于在 LB1 中选择了项目 2 和 3 - 在 LB2 和 LB3 中也应该选择项目 2 和 3。 But that's not what's happening.但事实并非如此。

在此处输入图像描述

======================================== UPDATE ========================================更新

How do I replicate the behavior when the user selects the items in ListBox2 or ListBox3当用户选择 ListBox2 或 ListBox3 中的项目时,如何复制行为

    private void listBox_1_SelectedIndexChanged_(object sender, EventArgs e)
    {
        listBox_2.ClearSelected();
        listBox_3.ClearSelected();

        int userSelectedIndex = listBox_1.Items.Count;
        if (listBox_1.SelectedIndices.Count > 0)
        {
            for (int count = 0; count < listBox_1.Items.Count; count++)
            {
                // Determine if the item is selected.
                if (listBox_1.GetSelected(count) == true)
                {
                    if (count <= listBox_2.Items.Count)
                        listBox_2.SetSelected(count, true);
                    if (count <= listBox_3.Items.Count)
                        listBox_3.SetSelected(count, true);
                }
                else if (listBox_1.GetSelected(count) == false)
                {
                    // Select all items that are not selected.
                    if (count <= listBox_2.Items.Count)
                        listBox_2.SetSelected(count, false);
                    if (count <= listBox_3.Items.Count)
                        listBox_3.SetSelected(count, false);
                }
            }
       }
    }

WHEN users selects items in ListBox 2, items in LB1 and LB3 should be selected as well.当用户选择 ListBox 2 中的项目时,也应选择 LB1 和 LB3 中的项目。

    private void listBox_2_SelectedIndexChanged(object sender, EventArgs e)
    {
        listBox_1.ClearSelected();  // ITS giving error here.
        listBox_3.ClearSelected(); 
           
        int userSelectedIndex = listBox_2.Items.Count;
        if (listBox_2.SelectedIndices.Count > 0)
        {
            for (int count = 0; count < listBox_2.Items.Count; count++)
            {
                // Determine if the item is selected.
                if (listBox_2.GetSelected(count) == true)
                {
                    if (count <= listBox_1.Items.Count)
                        listBox_1.SetSelected(count, true);
                    if (count <= listBox_3.Items.Count)
                        listBox_3.SetSelected(count, true);
                }
                else if (listBox_2.GetSelected(count) == false)
                {
                    // Select all items that are not selected.
                    if (count <= listBox_1.Items.Count)
                        listBox_1.SetSelected(count, false);
                    if (count <= listBox_3.Items.Count)
                        listBox_3.SetSelected(count, false);
                }
            }
       }
    }

It is unclear where this code is called.目前还不清楚这段代码是在哪里调用的。 I would simply clear the other list2 and list3 boxes and then set the same selected indexes as the ones in list box 1... some thing like...我会简单地清除其他 list2 和 list3 框,然后将选定的索引设置为与列表框 1 中的索引相同的索引……类似……

listBox_2.ClearSelected();
listBox_3.ClearSelected();
foreach (int selectedItem in listBox_1.SelectedIndices) {
  listBox_2.SetSelected(selectedItem, true);
  listBox_3.SetSelected(selectedItem, true);
}

Edit per OP comment…根据 OP 评论编辑...

I am still not following what the requirements are, however, if you want all the ListBoxes to have the “same selected INDEXES” in each ListBox , then the code below may help.我仍然没有遵循要求,但是,如果您希望所有ListBoxes在每个ListBox中都具有“相同的选定索引”,那么下面的代码可能会有所帮助。

One possible issue you may have with your current solution is a possible circular reference.当前解决方案可能存在的一个问题是可能的循环引用。 Example, In the listBox_1_SelectedIndexChanged code, there is line of code…例如,在listBox_1_SelectedIndexChanged代码中,有一行代码......

listBox_2.SetSelected(count, true);

… this sets the selected index in list box 2 which will fire the listBox_2_SelectedIndexChanged event. …这将在列表框 2 中设置选定索引,这将触发listBox_2_SelectedIndexChanged事件。 Then… inside that event code you have the code…然后......在那个事件代码中你有代码......

listBox_1.SetSelected(count, true);

… which will fire the list box 1 event “again.” …这将“再次”触发列表框 1 事件。

It should be obvious that this may create an infinite circular reference where each event simply calls the other event and never ends.很明显,这可能会创建一个无限循环引用,其中每个事件都简单地调用另一个事件并且永远不会结束。

Given that the code in the event needs to possibly “set” an index in one of the other ListBoxes , then it may be necessary to turn-off/unsubscribe the list box from its SelectedIndexChanged event before the code sets the index and then turn-on/re-subscribe to the event after the index has been set.鉴于事件中的代码可能需要在其他ListBoxes之一中“设置”索引,因此可能需要在代码设置索引之前关闭/取消订阅列表框的SelectedIndexChanged事件,然后打开 -在设置索引后打开/重新订阅事件。

Therefore in the solution below, Since the same event is used for ALL three ListBoxes , the code turns-off/un-subscribes from each ListBox event before the code starts, and then turns-on/re-subscribes to the events when done.因此在下面的解决方案中,由于同一事件用于所有三个ListBoxes ,代码在代码开始之前关闭/取消订阅每个ListBox事件,然后在完成后打开/重新订阅事件。

In addition, as @zaggler correctly notes, checking if the ListBox actually HAS an index of the given value, it becomes necessary to check that the index is in a valid range before setting the index to avoid an index out of bounds exception.此外,正如@zaggler 正确指出的那样,检查ListBox是否实际上具有给定值的索引,有必要在设置索引之前检查索引是否在有效范围内以避免索引越界异常。

private void listBox_SelectedIndexChanged(object sender, EventArgs e) {
  listBox_1.SelectedIndexChanged -= new EventHandler(listBox_SelectedIndexChanged);
  listBox_2.SelectedIndexChanged -= new EventHandler(listBox_SelectedIndexChanged);
  listBox_3.SelectedIndexChanged -= new EventHandler(listBox_SelectedIndexChanged);
  ListBox lb_1 = (ListBox)sender;
  ListBox lb_2;
  ListBox lb_3;
  switch (lb_1.Name) {
    case "listBox_1":
      lb_2 = listBox_2;
      lb_3 = listBox_3;
      break;
    case "listBox_2":
      lb_2 = listBox_1;
      lb_3 = listBox_3;
      break;
    default:
      lb_2 = listBox_1;
      lb_3 = listBox_2;
      break;
  }
  lb_2.ClearSelected();
  lb_3.ClearSelected();

  foreach (int selectedItem in lb_1.SelectedIndices) {
    if (selectedItem < lb_2.Items.Count) {
      lb_2.SetSelected(selectedItem, true);
    }
    if (selectedItem < lb_3.Items.Count) {
      lb_3.SetSelected(selectedItem, true);
    }
  }
  listBox_1.SelectedIndexChanged += new EventHandler(listBox_SelectedIndexChanged);
  listBox_2.SelectedIndexChanged += new EventHandler(listBox_SelectedIndexChanged);
  listBox_3.SelectedIndexChanged += new EventHandler(listBox_SelectedIndexChanged);
}

Here Selection of items in LB1 should control the selection in LB2 and LB3.此处 LB1 中项目的选择应控制 LB2 和 LB3 中的选择。 Now, since Item 2 and 3 are selected in LB1 - items 2 and 3 should be selected in LB2 and LB3 as well.现在,由于在 LB1 中选择了项目 2 和 3 - 在 LB2 和 LB3 中也应该选择项目 2 和 3。 But that's not what's happening.但事实并非如此。

I would recommend looping through all items of listBox_1 ...我建议循环遍历listBox_1的所有项目 ...

for (int idx = 0; idx <= listBox_1.Items.Count - 1; idx++)
            {
                if (listBox_1.GetSelected(idx))
                {
                    if (idx <= listBox_2.Items.Count)
                        listBox_2.SetSelected(idx, true);
                    if (idx <= listBox_3.Items.Count)
                        listBox_3.SetSelected(idx, true);
                }
                else
                {
                    if (idx <= listBox_2.Items.Count)
                        listBox_2.SetSelected(idx, false);
                    if (idx <= listBox_3.Items.Count)
                        listBox_3.SetSelected(idx, false);
                }
            }

Please note, I put a check as well to make sure the index would exist, otherwise it could throw an error if the index doesn't exist.请注意,我也进行了检查以确保索引存在,否则如果索引不存在它可能会抛出错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM