title: C# - Move TextBox to ListBox and be able to use Add/Remove Buttons
body: <p>I have 3 textboxes that have certain values in them. The values are split up depending on what the strings contain using different regex.</p>
  
  <p>These textboxes are in the background and the user will not see them.. However, I <em>do</em> want the user to see the listboxes that correspond to each textbox.  <strong><em>That is what the code below is:</em></strong></p>
  
  <pre><code>    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();
      }
  </code></pre>
  
  <hr>
  
  <p><em><strong>HOWEVER</em></strong>, my problem with this is that I cannot move each item in the list... What I mean is that I want to be able to have <code>Move up</code>, <code>Move down</code>,<code>Move left</code>, and <code>Move right</code> buttons.  The <code>Move up</code> and <code>Move down</code> buttons will allow the user to move the selected item(s) up or down <em>(to change the order of the items)</em> in the specified list.  The <code>Move left</code> and <code>Move right</code> buttons will allow the user to <strong>move</strong> the item on the current list to the list to the "right" or "left" of the current list.</p>
  
  <p><em><strong>VISUAL LAYOUT OF LISTBOXES:</em></strong></p>
  
  <pre><code>placementOneListBox                userDefinedListBox                placementTwoListBox
  |                 |                |                 |               |                 |
  |                 |                |                 |               |                 |
  |                 |                |                 |               |                 |
  |                 |                |                 |               |                 |
  |_________________|                |_________________|               |_________________|
  </code></pre>
  
  <p><em>and</em> I get the <strong><em>error</em></strong>:</p>
  
  <pre><code>"Items collection cannot be modified when the DataSource property is set."
  </code></pre>
  
  <hr>
  
  <p><em><strong>Move Up Button CODE:</em></strong></p>
  
  <pre><code>    private void moveUpButton_Click(object sender, EventArgs e)
      {
          if (placementOneListBox.SelectedIndex != 0 &amp;&amp; placementOneListBox.SelectedIndex != -1)
          {
              object item = placementOneListBox.SelectedItem;
              int index = placementOneListBox.SelectedIndex;
              placementOneListBox.Items.RemoveAt(index);
              placementOneListBox.Items.Insert(index - 1, item);
          }
      }
  </code></pre>
  
  <hr>
  
  <p><em><strong>Move Right Button CODE:</em></strong></p>
  
  <pre><code>    private void moveRightButton_Click(object sender, EventArgs e)
      {
          for (int i = 0; i &lt; placementTwoListBox.Items.Count; i++)
          {
              userDefinedListBox.Items.Add(placementTwoListBox.Items[i].ToString());
              placementTwoListBox.Items.Remove(placementTwoListBox.SelectedItem);
          }
      }
  </code></pre>
  
  <hr>
  
  <p><em><strong>QUESTIONS:</em></strong></p>
  
  <ul>
  <li>Is there a way to go about this where I can modify the DataSource property?</li>
  <li>Does anyone want to take a crack at this?</li>
  <li>How can I change my listFormatHelper() function to do what I need it to do and allow the buttons to work without the error above?</li>
  </ul>