C# - 将文本框移动到列表框并能够使用添加/删除按钮
我有 3 个文本框,其中包含某些值。这些值根据字符串包含的内容使用不同的正则表达式进行分割。
这些文本框位于后台,用户不会看到它们。但是,我确实希望用户看到与每个文本框对应的列表框。 这就是下面的代码:
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();
}
但是,我的问题是我无法移动其中的每个项目列表...我的意思是我希望能够上移
、下移
、向左移动
和向右移动
按钮。 上移
和下移
按钮将允许用户向上或向下移动所选项目(以更改项目的顺序) em> 在指定列表中。 向左移动
和向右移动
按钮将允许用户将当前列表上的项目移动到列表的“右”或“当前列表的左侧”。
列表框的视觉布局:
placementOneListBox userDefinedListBox placementTwoListBox
| | | | | |
| | | | | |
| | | | | |
| | | | | |
|_________________| |_________________| |_________________|
并且我收到错误:
"Items collection cannot be modified when the DataSource property is set."
< strong>向上移动按钮代码:
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);
}
}
向右移动按钮代码:
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);
}
}
问题:
- 有没有办法解决这个问题,我可以修改数据源 财产?
- 有人想尝试一下吗?
- 如何更改 listFormatHelper() 函数以执行我需要它执行的操作并允许按钮正常工作而不会出现上述错误?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您有两个选择:
Items
属性来填充列表。然后使用Items
属性管理订单,就像您当前正在尝试执行的那样。You have two options:
Items
property. Then manage the order using theItems
property, as you are currently trying to do.