I have 3 textboxes that have certain values in them. The values are split up depending on what the strings contain using different regex.
These textboxes are in the background and the user will not see them.. However, I do want the user to see the listboxes that correspond to each textbox. That is what the code below is:
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();
}
HOWEVER, 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 Move up, Move down,Move left, and Move right buttons. The Move up and Move down buttons will allow the user to move the selected item(s) up or down (to change the order of the items) in the specified list. The Move left and Move right buttons will allow the user to move the item on the current list to the list to the “right” or “left” of the current list.
VISUAL LAYOUT OF LISTBOXES:
placementOneListBox userDefinedListBox placementTwoListBox
| | | | | |
| | | | | |
| | | | | |
| | | | | |
|_________________| |_________________| |_________________|
and I get the error:
"Items collection cannot be modified when the DataSource property is set."
Move Up Button CODE:
private void moveUpButton_Click(object sender, EventArgs e)
{
if (placementOneListBox.SelectedIndex != 0 && placementOneListBox.SelectedIndex != -1)
{
object item = placementOneListBox.SelectedItem;
int index = placementOneListBox.SelectedIndex;
placementOneListBox.Items.RemoveAt(index);
placementOneListBox.Items.Insert(index - 1, item);
}
}
Move Right Button CODE:
private void moveRightButton_Click(object sender, EventArgs e)
{
for (int i = 0; i < placementTwoListBox.Items.Count; i++)
{
userDefinedListBox.Items.Add(placementTwoListBox.Items[i].ToString());
placementTwoListBox.Items.Remove(placementTwoListBox.SelectedItem);
}
}
QUESTIONS:
- Is there a way to go about this where I can modify the DataSource property?
- Does anyone want to take a crack at this?
- 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?
You have two options:
Itemsproperty. Then manage the order using theItemsproperty, as you are currently trying to do.