Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6137727
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T17:45:46+00:00 2026-05-23T17:45:46+00:00

Right now I have 3 RichTextBox(es) with text in them. I am taking this

  • 0

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

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?
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T17:45:46+00:00Added an answer on May 23, 2026 at 5:45 pm

    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?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

right now i have this <script type='text/javascript'> $(#beau).click(function(){ $(#beau).animate({margin-Top: 738px}, fast); }); </script> and
Good day! I right now have a function the drags an element from a
Right now I have this SQL query which is valid but always times out:
Right now I have double numba = 5212.6312 String.Format({0:C}, Convert.ToInt32(numba) ) This will give
If I have a RichTextBox that is loaded from a file containg: TEXT MORETEXT
right now I have this pulling data for the current week, but the week
Right now I have a database (about 2-3 GB) in PostgreSQL, which serves as
Right now I have an SSIS package that runs every morning and gives me
Right now I have the following in my .vimrc : au BufWritePost *.c,*.cpp,*.h !ctags
Right now I have a few new applications being developed against an Oracle Database,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.