C# - 使用ListBox添加/删除

the*_*Guy 3 listbox move add winforms

现在我有3个包含文本的RichTextBox.我从这些框中获取此文本并拆分每一行并将每个单独的行添加到其对应的ListBox.这是我的代码:

private void listFormatHelper()
{
    // Splits the lines in the rich text boxes
    var listOneLines = placementOneRichTextBox.Text.Split('\n');
    var listTwoLines = placementTwoRichTextBox.Text.Split('\n');
    var listUserLines = userDefinedRichTextBox.Text.Split('\n');

    // Resest the text in the listboxes
    placementOneListBox.ResetText();
    placementTwoListBox.ResetText();
    userDefinedListBox.ResetText();

    // Set the selection mode to multiple and extended.
    placementOneListBox.SelectionMode = SelectionMode.MultiExtended;
    placementTwoListBox.SelectionMode = SelectionMode.MultiExtended;
    userDefinedListBox.SelectionMode = SelectionMode.MultiExtended;

    // Shutdown the painting of the ListBox as items are added.
    placementOneListBox.BeginUpdate();
    placementTwoListBox.BeginUpdate();
    userDefinedListBox.BeginUpdate();

    // Display the items in the listbox.
    placementOneListBox.DataSource = listOneLines;
    placementTwoListBox.DataSource = listTwoLines;
    userDefinedListBox.DataSource = listUserLines;

    // Allow the ListBox to repaint and display the new items.
    placementOneListBox.EndUpdate();
    placementTwoListBox.EndUpdate();
    userDefinedListBox.EndUpdate();
}
Run Code Online (Sandbox Code Playgroud)

在上面的代码之后,每个ListBox都在单独的行中包含指定的数据.但是,我要做的是添加按钮按钮,单击时按钮将所选列表项移动到指定的ListBox.


列表框的可视布局:

placementOneListBox                userDefinedListBox                placementTwoListBox
|                 |                |                 |               |                 |
|                 |                |                 |               |                 |
|                 |                |                 |               |                 |
|                 |                |                 |               |                 |
|_________________|                |_________________|               |_________________|
Run Code Online (Sandbox Code Playgroud)

所以,我试图做的是有一个按钮,上面写着"向右移动"或"向左移动",它需要当前选择的项目(最好的项目为多选),并将其移动到左侧或到右侧列表框.但是,对于placementOneListBox,"向左移动"按钮将不起作用,对于placementTwoListBox,"向右移动"按钮将不起作用.我在下面尝试过这种方式(不起作用):

private void placementOneMoveRightButton_Click(object sender, EventArgs e)
{
    var currentItemText = placementOneListBox.SelectedValue.ToString();
    var currentItemIndex = placementOneListBox.SelectedIndex;

    userDefinedListBox.Items.Add(currentItemText);
    placementOneListBox.Items.Remove(currentItemIndex);
    placementOneListBox.Items.RemoveAt(placementOneListBox.Items.IndexOf(placementOneListBox.SelectedItem));
}
Run Code Online (Sandbox Code Playgroud)

我也试过这种方式(也没用):

private void placementOneMoveRightButton_Click(object sender, EventArgs e)
{
    string str;
    str = placementOneListBox.SelectedItems.ToString();
    placementOneListBox.Items.Remove(placementOneListBox.SelectedItems);
    userDefinedListBox.Items.Add(str);
}
Run Code Online (Sandbox Code Playgroud)

他们为什么不工作的原因:

每当我运行程序并单击"向右移动"按钮(对于上面的任何代码情况),我都会收到以下错误:

"Items collection cannot be modified when the DataSource property is set."
Run Code Online (Sandbox Code Playgroud)

质询

  • 有谁知道我在做错了什么?
  • 有人可以显示/解释"DataSource属性"发生了什么,以及我如何解决它?

Ice*_*ind 5

您试图在数据集绑定到列表框时修改数据集.您需要做的是重新创建数据集,然后将新数据集绑定到列表框.

对不起科尔顿,在工作中开了个会.

我不确定您使用的是哪种数据,但这是一个删除名称并为ListBox添加名称的示例:

private class Names
{
    public string Name { get; set; }

    public Names(string name)
    {
        Name = name;
    }
}

private void Form1_Load(object sender, EventArgs e) // Form Load Event
{
    List<Names> names = new List<Names>();

    names.Add(new Names("John"));
    names.Add(new Names("Suzy"));
    names.Add(new Names("Mary"));
    names.Add(new Names("Steve"));

    listBox1.DataSource = names;
    listBox1.DisplayMember = "Name";
    listBox1.ValueMember = "Name";
}

private void button1_Click(object sender, EventArgs e)
{
    List<Names> names = new List<Names>();
    foreach (var item in listBox1.Items)
    {
        Names name = (Names)item;
        if (name.Name.Equals("Mary")) // Remove Mary
            continue;

        names.Add(name);
    }

    names.Add(new Names("Joe")); // Add Joe

    listBox1.DataSource = names;
    listBox1.DisplayMember = "Name";
    listBox1.ValueMember = "Name";
}
Run Code Online (Sandbox Code Playgroud)

所以我正在做的是在Form Load事件中,我用新名称填充我的列表框并将数据源设置为我的List对象.单击该按钮时,我正在创建一个新列表,并使用相同的名称填充,除了可怜的玛丽.她不在名单之列.然后我将Add Joe添加到列表中,然后再次设置数据源.重要的是,一旦将数据源绑定到列表框,就无法以任何方式修改该数据源.您必须创建一个新数据源,然后重新绑定到新源.这现在更有意义了吗?