【问题标题】:What control should I use for multiple selection of item in winforms C#?在winforms C#中,我应该使用什么控件来多选项目?
【发布时间】:2015-05-23 14:29:03
【问题描述】:

在考虑使用哪个控件来实现我的目的时,我真的很困惑。

我有一个项目列表,从项目 1 到项目 10。用户可以按任意顺序选择 4 或 5 个项目。

现在用户选择的项目必须以相同的顺序分开。

例如,如果用户按照以下顺序选择项目,item4,item8,item3和item2。

我想要它以相同的顺序。第 4 项,第 8 项,第 3 项,第 2 项。

如何在 winforms 控件中实现这一点?

【问题讨论】:

  • 你试过ListBox控件了吗?
  • 如果选择顺序真的很重要,那么让用户知道他选择了正确的顺序是非常重要的。并且可以修正错误。其标准 UI 是 两个 列表框。一个显示可用项目,另一个显示选定项目和订单。在列表框之间来回移动项目的两个按钮。你可能见过this before
  • 是的,我尝试了列表框,正如“hans passant”所说。但问题是我在表单页面中有很多其他控件。如果我使用 2 列表框方法,表单页面会随着它占据更多部分而下降和下降。除了 2 列表框方法之外还有其他选择吗
  • 一个列表框就可以搞定。如果它适合您的需要,请告诉我更多解释。
  • @RaizeAhamed 如果您的表单空间有限,您可以在User Experience 上询问想法。

标签: c# winforms visual-studio-2010


【解决方案1】:

这不是一个很好的解决方案,但我按照你的要求写了它。 根据需要将 ListBox 的 SelectionMode 设置为 MultiExtendedMultiSimple
然后在 ListBox 的SelectedIndexChanged 事件中编写此代码:

    List<string> orderedSelection = new List<string>();
    bool flag = true;
    private void listBox3_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (flag)
        {
            flag = false;
            var list1 = listBox3.SelectedItems.Cast<string>().ToList();
            if (listBox3.SelectedItems.Count > orderedSelection.Count)
            {
                orderedSelection.Add(list1.Except(orderedSelection).First());
            }
            else if (listBox3.SelectedItems.Count < orderedSelection.Count)
            {
                orderedSelection.Remove(orderedSelection.Except(list1).First());
            }

            var list2 = listBox3.Items.Cast<string>().Except(list1).ToList();
            listBox3.Items.Clear();
            for (int i = 0; i < list1.Count; i++)
            {
                listBox3.Items.Add(list1[i]);
                listBox3.SelectedIndex = i;
            }
            foreach (string s in list2)
            {
                listBox3.Items.Add(s);
            }
            flag = true;
        }
    }

当用户选择一个项目时,它来到列表的第一个,其余的项目在下一个。

此外,还有另一种方法。您可以使用带有两个额外按钮的CheckedListBox 来上下移动所选项目。因此用户可以更改所选项目的顺序。

【讨论】:

    【解决方案2】:

    此解决方案使用 CheckListBox 的 ItemCheck 事件以及私有 List 来跟踪单击项目的顺序。

    protected List<string> clickOrderList = new List<string>();
    
    private void Form1_Load(object sender, EventArgs e)
    {
        // Populate the checked ListBox
        this.checkedListBox1.Items.Add("Row1");
        this.checkedListBox1.Items.Add("Row2");
        this.checkedListBox1.Items.Add("Row3");
        this.checkedListBox1.Items.Add("Row4");
    }
    
    private void checkedListBox1_ItemCheck(object sender, ItemCheckEventArgs e)
    {
        if (sender != null && e != null)
        {
            // Get the checkListBox selected time and it's CheckState
            CheckedListBox checkListBox = (CheckedListBox)sender;
            string selectedItem = checkListBox.SelectedItem.ToString();
    
            // If curent value was checked, then remove from list
            if (e.CurrentValue == CheckState.Checked &&
                clickOrderList.Contains(selectedItem))
            {
                clickOrderList.Remove(selectedItem);
            }
            // else if new value is checked, then add to list
            else if (e.NewValue == CheckState.Checked &&
                !clickOrderList.Contains(selectedItem))
            {
                clickOrderList.Insert(0, selectedItem);
            }
        }
    }
    
    private void ShowClickOrderButton_Click(object sender, EventArgs e)
    {
        StringBuilder sb = new StringBuilder();
        foreach (string s in clickOrderList)
        {
            sb.AppendLine(s);
        }
    
        MessageBox.Show(sb.ToString());
    }
    

    【讨论】:

      猜你喜欢
      • 2017-05-11
      • 1970-01-01
      • 1970-01-01
      • 2014-08-15
      • 1970-01-01
      • 2018-12-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多