如何将光标移动到C#中富文本框中的下一个单词

时间:2012-07-11 09:54:31

标签: c#

在C#中,如何将光标移动到富文本框中的下一个单词?例如

给出句子

“他是个男孩”

让我们假设当前光标位于“是”之前,即“He”之后,我想将它移动到“a”之前的位置,即“is”之后。

可以richtextbox.SelectionStart用于执行此类操作吗?

4 个答案:

答案 0 :(得分:1)

您应该尝试使用.Find(Char)方法找到下一个空格的位置。

int x = richtextbox.Find(' ');
richtextbox.SelectionStart = x;
richtextbox.Focus();

没有可能测试它,但我应该工作。

答案 1 :(得分:0)

如果你不介意使用SendKeys,那么下面的例子将达到你的要求

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        richTextBox1.Focus();
        SendKeys.SendWait("^{LEFT}");
    }

    private void button2_Click(object sender, EventArgs e)
    {
        richTextBox1.Focus();
        SendKeys.SendWait("^{RIGHT}");
    }
}

答案 2 :(得分:0)

这应该有效:

int curr = richTextBox1.SelectionStart;

int x = Regex.Match(richTextBox1.Text.Substring(curr), @"\s[^\s]").Index;

if (x != 0)
    richTextBox1.SelectionStart = x + curr + 1;

richTextBox1.Focus();

答案 3 :(得分:0)

如果你的目的是搜索一个单词;突出显示它并将光标移动到找到的单词: 注意:搜索按钮用作“每次点击”;所以每次点击;它将搜索richtextBox文本文档中的下一个匹配单词。

    // Search text and Highlight it (Per Click)
    // Manually add the "FindText"
    public int FindText(string txtToSearch, int searchStart, int searchEnd)
    {
        // Unselect the previously searched string
        if (searchStart > 0 && searchEnd > 0 && IndexOfSearchText >= 0)
        { rtb.Undo(); }

        // Set the return value to -1 by default.
        int retVal = -1;

        // A valid starting index should be specified.
        // if indexOfSearchText = -1, the end of search
        if (searchStart >= 0 && IndexOfSearchText >= 0)
        {
            // A valid ending index
            if (searchEnd > searchStart || searchEnd == -1)
            {
                // Find the position of search string in RichTextBox
                IndexOfSearchText = rtb.Find(txtToSearch, searchStart, searchEnd, RichTextBoxFinds.None);
                // Determine whether the text was found in richTextBox1.
                if (IndexOfSearchText != -1)
                {
                    // Return the index to the specified search text.
                    retVal = IndexOfSearchText;
                }
            }
        }
        return retVal;
    }

    // Button to Perform the Search (By Click)
    private void btn_Search_Click(object sender, EventArgs e)
    {
        int startIndex = 0;

        if (txt_Search.Text.Length > 0) startIndex = FindText(txt_Search.Text.Trim(), start, rtb.Text.Length);

        // If string was found in the RichTextBox, highlight it
        if (startIndex >= 0)
        {
            int x = rtb.Find(txt_Search.Text);
            rtb.SelectionStart = x;
            rtb.Focus();
            // Set the highlight color as red
            rtb.SelectionBackColor = Color.LightYellow;  // Remove to avoid minor Bug
            rtb.SelectionColor = Color.Black;            // Remove to avoid Minor Bug
            // Find the end index. End Index = number of characters in textbox
            int endindex = txt_Search.Text.Length;
            // Highlight the search string
            rtb.Select(startIndex, endindex);
            // mark the start position after the position of
            // last search string
            start = startIndex + endindex;
        }

    }

    // TextBox to Search
    private void txt_Search_TextChanged(object sender, EventArgs e)
    {
        start = 0;
        IndexOfSearchText = 0;
    }
}

}

希望这有帮助。