开发者

C# - Add/Remove with ListBoxes

Right now I have 3 RichTextBox(es) with text in them. I am taking this text from these boxes and splitting each line and adding each separate line to its corresponding ListBox. Here is my code for that:

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;
    u开发者_JAVA技巧serDefinedListBox.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();
}

After the above code, each ListBox has the specified data in it on separate lines. However, what I am trying to do is add buttons buttons, that when clicked move the selected list item to the specified ListBox.


VISUAL LAYOUT OF LISTBOXES:

placementOneListBox                userDefinedListBox                placementTwoListBox
|                 |                |                 |               |                 |
|                 |                |                 |               |                 |
|                 |                |                 |               |                 |
|                 |                |                 |               |                 |
|_________________|                |_________________|               |_________________|

So, what I am trying to do is have a button that says "move right" or "move left" and it takes the currently selected item (preferably items for multiselection) and moves it to either the left or to the right ListBox. However, for the placementOneListBox the "move left" button will not work and for the placementTwoListBox the "move right" button will not work. I tried this way below (which did not work):

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

I also tried this way (which also did not work):

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

THE REASON WHY THEY DO NOT WORK:

Whenever I am running the program and I click the "move right" button (for either code cases above) I get the following error:

"Items collection cannot be modified when the DataSource property is set."

QUESTIONS

  • Does anyone know what I am doing wrong in this?
  • Can someone show/explain what is happening with the "DataSource property" and how I can get around it?


You are trying to modify the dataset while it is bound to the listbox. What you need to do instead is recreate the dataset and then bind that new dataset to the listbox.

Sorry Colton, had a meeting at work.

I am not sure what kind of data you are working with, but here is an example of removing a name and adding a name to a 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";
}

So what I'm doing is in the Form Load event, I'm populating my listbox with fresh names and setting the datasource to my List object. When the button is clicked, I am creating a NEW List and populating with the same names except for poor Mary. She's out of the list. I then Add Joe to the list and then set the datasource again. The important thing to take away from this is that once you have binded a data source to a listbox, you CANNOT modify that data source in any way. You must create a new datasource and then rebind to the new source. Does this make more sense now?


refer following article at code project : How to Move List Box Items to another List Box in C#.

also you can look at so quetion : how-to-remove-selected-items-from-listbox-when-a-datasource-is-assigned-to-it-in


Does this work:

private void placementOneMoveRightButton_Click(object sender, EventArgs e)
{
    for(int i = 0; i < placementOneListBox.SelectedItems.Count(); i++)
    {
        var item = placementOneListBox.Items[i];
        if(item.Selected)
        {
            placementOneListBox.Items.RemoveAt(i);
            userDefinedListBox.Items.Add(item);
        }
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜