1

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

给定句子

“他是个男孩”

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

可以richtextbox.SelectionStart用来执行这样的动作吗?

4

4 回答 4

0

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

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

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

于 2012-07-11T10:06:07.250 回答
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}");
    }
}
于 2012-07-11T10:08:00.340 回答
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();
于 2012-07-11T10:13:55.703 回答
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;
    }
}

}

希望这可以帮助。

于 2014-02-05T16:55:21.407 回答