C# - 将文本框移动到列表框并能够使用添加/删除按钮

发布于 2024-11-25 04:35:10 字数 3684 浏览 7 评论 0原文

我有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

逆光飞翔i 2024-12-02 04:35:10

您有两个选择:

  1. 不使用数据绑定;将数据源转换为对象序列,并通过将序列中的对象添加到 Items 属性来填充列表。然后使用 Items 属性管理订单,就像您当前正在尝试执行的那样。
  2. 修改数据源本身以改变顺序。如何执行此操作取决于您使用的数据源。

You have two options:

  1. Don't use data binding; convert the data source to a sequence of objects and populate the list(s) by adding the objects in the sequence to the Items property. Then manage the order using the Items property, as you are currently trying to do.
  2. Modify the data source itself to alter the order. How you do this depends on which data source you are using.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文