如何在Richtextbox中的逗号之间选择文本

时间:2013-07-16 18:39:47

标签: winforms c#-4.0 click richtextbox messagebox

在RichtextBox中,它们之间带有一些逗号。我必须找到我在整体中单击的单词。逗号之间的单词必须显示在messagebox中。

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

整个句子是lable text。如果我点击Sachin。那么邮件必须是

Sachin Tendulkar(40)

如果我点击sehwag messagebox必须显示

Virendra shewag.

必须显示逗号之间的字。 任何人请帮忙

1 个答案:

答案 0 :(得分:0)

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

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

您必须依赖给定Click Event的{​​{1}}(从现在起RichTextBox)才能获得richTextBox1的起始索引(Selection )。

您必须创建一个函数来从给定索引前后分析整个字符串,直到找到逗号或第一个/最后一个字符。此函数的输出将是第一个(selectionFirst)和最后一个(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;
}
相关问题