C# 在WinC窗体中,我应该使用什么控件进行多项选择?

C# 在WinC窗体中,我应该使用什么控件进行多项选择?,c#,winforms,visual-studio-2010,C#,Winforms,Visual Studio 2010,我真的不知道该用哪个控件来达到我的目的 我有一个项目列表,比如项目1到项目10。用户可以按任意顺序选择4或5项 现在,用户选择的项目必须按相同的顺序分开 例如,如果用户按以下顺序选择项目,则为item4、item8、item3和item2 我要同样的顺序。第四项、第八项、第三项、第二项 如何在winforms控件中实现这一点?这不是一个很好的解决方案,但我按照您的要求编写了它。 根据需要将列表框的SelectionMode设置为MultiExtend或MultiSimple。 然后在列表框的Se

我真的不知道该用哪个控件来达到我的目的

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

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

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

我要同样的顺序。第四项、第八项、第三项、第二项


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

这不是一个很好的解决方案,但我按照您的要求编写了它。 根据需要将列表框的SelectionMode设置为MultiExtend或MultiSimple。 然后在列表框的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上下移动所选项目。因此用户可以更改所选项目的顺序。

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

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

你试过ListBox控件吗?如果选择顺序真的很重要,那么用户可以知道他选择的顺序是正确的是非常重要的。并且可以修正错误。标准用户界面是两个列表框。一个显示可用项目,另一个显示选定项目和订单。两个按钮可在列表框之间来回移动项目。你可能见过。是的,我试过“汉斯·帕桑”说的listbox。但问题是我在表单页面中有很多其他控件。如果使用2 listbox方法,表单页面会随着占据更多部分而不断向下移动。除了2个列表框方法之外,还有其他选择吗?您可以使用一个列表框来完成。如果它符合你的需要,让我知道更多的解释。@RaizeAhamed如果你的表格空间有限,你可以向我征求意见。