在C#中,如何将光标移动到富文本框中的下一个单词?例如如何将光标移动到C#中富文本框中的下一个单词#
给出的句子
“他是个男孩”
让我们假设,目前“他”,我想之前将其移动到的位置之后,前“是”,即光标定位“一”即在“是”之后。
可以richtextbox.SelectionStart
用于执行此类操作吗?
在C#中,如何将光标移动到富文本框中的下一个单词?例如如何将光标移动到C#中富文本框中的下一个单词#
给出的句子
“他是个男孩”
让我们假设,目前“他”,我想之前将其移动到的位置之后,前“是”,即光标定位“一”即在“是”之后。
可以richtextbox.SelectionStart
用于执行此类操作吗?
,如果你不介意使用的SendKeys然后TEH下面的例子将实现您的要求
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}");
}
}
您应该尝试使用.Find(Char)方法找到下一个空格的位置。
int x = richtextbox.Find(' ');
richtextbox.SelectionStart = x;
richtextbox.Focus();
没有测试它的可能性,但我应该工作。
这应该工作:
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();
和两个连续的空格? – Steve 2012-07-11 10:19:50
固定。 @Steve,谢谢 – 2012-07-11 10:41:51
如果你的目的是为了寻找一个字;突出显示并将光标移动到找到的单词: 注意:搜索按钮的工作方式为“每次点击”;所以每次点击它将搜索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;
}
}
}
希望这有助于。
非常感谢您的帮助。有效!。实际上,我正在为URDU文本开发一个标记工具,并且在两个单词之间插入标记后,我希望光标在下两个标记之间自动移动。那么,用户可以省去手动移动光标的不方便 – 2012-07-13 06:53:04