【问题标题】:How to select text between commas in Richtextbox如何在 Richtextbox 中选择逗号之间的文本
【发布时间】:2013-07-17 04:30:40
【问题描述】:

在 RichtextBox 中,一些单词之间有逗号。我必须找到我在整体中单击的单词。并且逗号之间的单词必须显示在消息框中。

Ex:-  Sachin Tendulkar (40),Virendra shewag,Mahendra singh Dhoni(12)

整个句子是lable text。如果我点击Sachin。那么消息一定是

Sachin Tendulkar(40)

如果我点击sehwagmessagebox 必须显示

Virendra shewag.

必须显示逗号之间的单词。 有谁请帮忙

【问题讨论】:

    标签: winforms c#-4.0 click richtextbox messagebox


    【解决方案1】:

    您必须创建一个自定义算法来解决这个问题。你没有写任何东西,我不能为你写,因此我将只包括基本部分和需要考虑的最重要的问题:

    private void richTextBox1_Click(object sender, EventArgs e)
    {
        int selectionFirst = richTextBox1.SelectionStart;
    }
    

    您必须依靠给定RichTextBoxClick Event(从现在开始richTextBox1)来获取SelectionselectionFirst)的起始索引。

    您必须创建一个函数来向前和向后分析给定索引中的整个字符串,直到找到逗号或第一个/最后一个字符。此函数的输出将是第一个 (indexFirst) 和最后一个 (indexLast) 索引。

    通过这两个索引,您可以定义您想要的选择范围:

    richTextBox1.SelectionStart = indexFirst;
    richTextBox1.SelectionLength = indexLast - indexFirst;
    

    你有它。

    --------- 更新整个代码

    private int[] findFirstLastChar(int curIndex)
    {
        int[] outIndices = new int[3];
        string curText = richTextBox1.Text;
    
        if (curText.Trim().Length < 1)
        {
            return outIndices;
        }
    
        int maxVal = 0;
        int iniVal = curIndex;
        bool completed = false;
        int count0 = 0;
        do
        {
            count0 = count0 + 1;
    
            if (count0 == 1)
            {
                //Backwards
            }
            else
            {
                //Forwards
                maxVal = curText.Length - 1;
            }
    
            completed = false;
            int count1 = curIndex;
            if (count1 == maxVal)
            {
                outIndices[count0] = count1;
            }
            else
            {
                do
                {
                    if (count0 == 1)
                    {
                        count1 = count1 - 1;
                        if (count1 <= maxVal)
                        {
                            completed = true;
                        }
                    }
                    else
                    {
                        count1 = count1 + 1;
                        if (count1 >= maxVal)
                        {
                            completed = true;
                        }
                    }
    
                    if (count1 >= 0 && count1 <= curText.Length)
                    {
                        if (curText.Substring(count1, 1) == "," || completed)
                        {
                            outIndices[count0] = count1;
                            if ((count0 == 1 && !completed) || (count0 == 2 && completed))
                            {
                                outIndices[count0] = outIndices[count0] + 1;
                            }
                            break;
                        }
                    }
                } while (!completed);
            }
        } while (count0 < 2);
    
    
        return outIndices;
    }
    

    在点击事件中:

    private void richTextBox1_Click(object sender, EventArgs e)
    {
        int[] indices = findFirstLastChar(richTextBox1.SelectionStart);
    
        if (indices[1] <= indices[2])
        {
            richTextBox1.SelectionStart = indices[1];
            richTextBox1.SelectionLength = indices[2] - indices[1];
        }
    }
    

    【讨论】:

    • 是的,我知道。你必须建立一个函数来接受它。这就是我所说的“创建一个函数来分析给定索引中的整个字符串......”。这很容易。我将更新我的代码以包含一个小代码,向您展示如何继续,但不是整个功能......给 5 分钟。
    • 感谢您的宝贵回复。我已经完成了,但是我无法在逗号之间找到文本。我的输出是如果我点击 sachin,我会得到 sachin。但是,我需要逗号之间的整个单词.我会等的。请显示代码兄弟
    • 这里有一个示例代码。看?这很容易。您必须编写转发部分并调用此函数两次,或者将其放入循环中并让它执行一个然后另一个。
    • 我之前使用了上面的链接。谢谢兄弟。我会检查你的代码并回复你。
    猜你喜欢
    • 2023-01-04
    • 1970-01-01
    • 2020-01-15
    • 2023-03-25
    • 1970-01-01
    • 1970-01-01
    • 2012-02-13
    • 2014-11-02
    • 2020-02-20
    相关资源
    最近更新 更多