替换 C# 中富文本框中的一行

发布于 2024-12-18 14:48:36 字数 1875 浏览 2 评论 0原文

因此,我尝试在 Visual Studio 2010 中替换富文本框中的一行。对于一个项目,我正在制作一个菜单系统,当选择一个项目时,我需要更新显示该项目编号的文本框。我不知道该项目在列表中的具体位置,并且可能有多个项目显示相同的编号,因此我无法使用 .replace 方法。我可以获得该项目所在的行号,但是当我尝试使用用于查找行号的索引时,我收到一条错误消息“StartIndex 不能小于零”。这是我到目前为止的代码:

//跟踪每个项目的数量

    int[] mainItems = new int[10];

    //function for menu item buttons
    private void MenuButtonClick(object sender, EventArgs e)
    {
        int index = 0;
        Button btn = (Button)sender;
        string s = btn.Name.ToString();
        //if item doesnt already exist on order
        if (!textBoxItems.Text.Contains(s))
        {
            textBoxItems.Text += "\r\n• " + s;
            //track num of each item
            switch (s)
            {
                case "Tiramisu": mainItems[0]++;
                    break;
                case "Lazania": mainItems[1]++;
                    break;

            }
            textBoxCount.Text += "\r\n(1)";
        }
        else
        {
            int itemID = 0;
            switch (s)
            {
                case "Tiramisu": mainItems[0]++;
                    itemID = 0;
                    break;
                case "Lazania": mainItems[1]++;
                    itemID = 1;
                    break;

            }

           int lastIndex = 0;
            //get line number of item
            do
            {
                index = textBoxItems.Find(s, index + 1, RichTextBoxFinds.MatchCase);
                if (index != -1)
                {
                    lastIndex = index;
                }
            } while ((index != -1));
            //int lineNum = textBoxItems.GetLineFromCharIndex(index);
            //MessageBox.Show("" + lineNum);

            textBoxCount.Text.Remove(lastIndex, 2);
            textBoxCount.Text.Insert(lastIndex, "(" + mainItems[itemID] + ")");

        }

任何帮助表示赞赏!提前致谢

So I'm attempting to replace a line in a rich text box in visual studio 2010. For a project I'm making a menu system and when an item is selected I need to update the text box which shows the number of that item. I wont know the specific location where the item is in the list, and there could be multiple items that display the same number so i can't use the .replace method. I can get the line number that the item is on, but when i try to use the index that i used to find the line number i get an error saying "StartIndex cannot be less than zero." Here's the code i have so far:

//keep track of the number of each item

    int[] mainItems = new int[10];

    //function for menu item buttons
    private void MenuButtonClick(object sender, EventArgs e)
    {
        int index = 0;
        Button btn = (Button)sender;
        string s = btn.Name.ToString();
        //if item doesnt already exist on order
        if (!textBoxItems.Text.Contains(s))
        {
            textBoxItems.Text += "\r\n• " + s;
            //track num of each item
            switch (s)
            {
                case "Tiramisu": mainItems[0]++;
                    break;
                case "Lazania": mainItems[1]++;
                    break;

            }
            textBoxCount.Text += "\r\n(1)";
        }
        else
        {
            int itemID = 0;
            switch (s)
            {
                case "Tiramisu": mainItems[0]++;
                    itemID = 0;
                    break;
                case "Lazania": mainItems[1]++;
                    itemID = 1;
                    break;

            }

           int lastIndex = 0;
            //get line number of item
            do
            {
                index = textBoxItems.Find(s, index + 1, RichTextBoxFinds.MatchCase);
                if (index != -1)
                {
                    lastIndex = index;
                }
            } while ((index != -1));
            //int lineNum = textBoxItems.GetLineFromCharIndex(index);
            //MessageBox.Show("" + lineNum);

            textBoxCount.Text.Remove(lastIndex, 2);
            textBoxCount.Text.Insert(lastIndex, "(" + mainItems[itemID] + ")");

        }

Any help is appreciated! thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

小糖芽 2024-12-25 14:48:36

问题是你循环直到index == -1

while ((index != -1));

,然后立即使用该索引

int lineNum = textBoxItems.GetLineFromCharIndex(index);

我不确定while循环的逻辑到底是什么,但乍一看,你可能想在index == -1时打破。或者跟踪循环外的最后一个非负索引。

更新

要查找用户刚刚单击的项目,您需要更改循环以记录最后一个不为-1的索引。 while 循环的目的是找到 s 的最后一次出现,但是您必须将最后一次有效出现记录在单独的变量中(下面代码中的lastIndex)。您还必须测试循环完成后是否确实找到了某些内容。

以下是我如何实现这个逻辑:

        int lastIndex = -1;

        //get line number of item
        do
        {
            index = textBoxItems.Find(s, index + 1, RichTextBoxFinds.MatchCase);
            if (index != -1) {
               lastIndex = index;
            }
        } while ((index != -1));
        if (lastIndex !+ -1) {
            int lineNum = textBoxItems.GetLineFromCharIndex(lastIndex);
            //messageBox.Show(""+lineNum);
            textBoxCount.Text.Remove(lastIndex, 3);
            textBoxCount.Text.Insert(lastIndex, "(" + mainItems[itemID] + ")");
        }

The problem is that you loop until index == -1

while ((index != -1));

then immediately use that index

int lineNum = textBoxItems.GetLineFromCharIndex(index);

I'm not sure exactly what the logic of the while loop is, but at first glance, you may want to break out if index == -1. Or keep track of the last non-negative index outside the loop.

Update

To find the item the user just clicked on, you need to change the loop to record the last index that is not -1. The purpose of the while loop is to find the last occurrence of s, but you have to record the last valid occurrence in a separate variable (lastIndex in the code below). You also must test that something was actually found after the loop completes.

Here is how I would implement this logic:

        int lastIndex = -1;

        //get line number of item
        do
        {
            index = textBoxItems.Find(s, index + 1, RichTextBoxFinds.MatchCase);
            if (index != -1) {
               lastIndex = index;
            }
        } while ((index != -1));
        if (lastIndex !+ -1) {
            int lineNum = textBoxItems.GetLineFromCharIndex(lastIndex);
            //messageBox.Show(""+lineNum);
            textBoxCount.Text.Remove(lastIndex, 3);
            textBoxCount.Text.Insert(lastIndex, "(" + mainItems[itemID] + ")");
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文