C# C |如何通过光标位置选择文本框中的单词?
在Windows窗体中,使用C,如何选择如中所示的文本,实际高亮显示文本,使.SelectedText属性可以访问文本?基于光标位置的单词 这就是我要做的。我有一个文本框,用户当前可以通过突出显示来选择一个单词。然后他们可以对单词执行各种操作,但我想让它更简单 我希望这样做,他们可以简单地将光标放在单词内,应用程序将选择光标所在的单词C# C |如何通过光标位置选择文本框中的单词?,c#,winforms,textbox,cursor,C#,Winforms,Textbox,Cursor,在Windows窗体中,使用C,如何选择如中所示的文本,实际高亮显示文本,使.SelectedText属性可以访问文本?基于光标位置的单词 这就是我要做的。我有一个文本框,用户当前可以通过突出显示来选择一个单词。然后他们可以对单词执行各种操作,但我想让它更简单 我希望这样做,他们可以简单地将光标放在单词内,应用程序将选择光标所在的单词 提前谢谢 使用SelectionStart和SelectionLength属性。 您可以使用SelectionStart和SelectionLength,但您可能
提前谢谢 使用SelectionStart和SelectionLength属性。 您可以使用SelectionStart和SelectionLength,但您可能需要从光标位置查找下一个空格,然后反转文本框的内容并从更改的光标位置查找下一个空格,然后使用上述两种方法 这也会起作用
int cursorPosition = textBox1.SelectionStart;
int nextSpace = textBox1.Text.IndexOf(' ', cursorPosition);
int selectionStart = 0;
string trimmedString = string.Empty;
// Strip everything after the next space...
if (nextSpace != -1)
{
trimmedString = textBox1.Text.Substring(0, nextSpace);
}
else
{
trimmedString = textBox1.Text;
}
if (trimmedString.LastIndexOf(' ') != -1)
{
selectionStart = 1 + trimmedString.LastIndexOf(' ');
trimmedString = trimmedString.Substring(1 + trimmedString.LastIndexOf(' '));
}
textBox1.SelectionStart = selectionStart;
textBox1.SelectionLength = trimmedString.Length;
希望这有助于:
if (string.IsNullOrEmpty(textBox1.Text))
{
return;
}
int cursorPos = textBox1.SelectionStart;
int firstPos = 0;
int lastPost = 0;
// If the current cursor is at the end of the string, try to go backwards
string currentChar = cursorPos == textBox1.Text.Length ? textBox1.Text.Substring(cursorPos - 1, 1) : textBox1.Text.Substring(cursorPos, 1);
if (currentChar == " ")
{
cursorPos--;
}
// Iterate to the first position where a space is
for (int i = cursorPos; i > 0; i--)
{
// Get the current character
currentChar = i == textBox1.Text.Length ? textBox1.Text.Substring(i - 1, 1) : textBox1.Text.Substring(i, 1);
if (currentChar == " ")
{
firstPos = i+1;
break;
}
}
for (int i = cursorPos; i <= textBox1.Text.Length; i++)
{
if (i == textBox1.Text.Length)
{
lastPost = i;
}
else
{
// Get the current character
currentChar = textBox1.Text.Substring(i, 1);
if (currentChar == " ")
{
lastPost = i;
break;
}
}
}
textBox1.SelectionStart = firstPos;
textBox1.SelectionLength = lastPost - firstPos;
textBox1.Focus();
对于本例,您需要一个文本框、textBox1和一个按钮来存放代码。
如果你需要帮助,请告诉我
编辑:稍微修改了代码并测试了所有场景。希望有帮助 实际上我这里有一个更简单的方法
Dim intCursor As Integer = txtInput.SelectionStart
Dim intStart As Int32 = CInt(IIf(intCursor - 1 < 0, 0, intCursor - 1))
Dim intStop As Int32 = intCursor
intStop = txtInput.Text.IndexOf(" ", intCursor)
intStart = txtInput.Text.LastIndexOf(" ", intCursor)
If intStop < 0 Then
intStop = txtInput.Text.Length
End If
If intStart < 0 Then
intStart = 0
End If
debug.print( txtInput.Text.Substring(intStart, intStop - intStart).Trim)
隐马尔可夫模型。。。我可以通过SelectionStart获取光标位置。如果他们把光标放在单词的中间,那在我的情况下更有可能。我不清楚如何选择这个词。我来自一个网络开发背景。。。。提前感谢。使用.Text并查找空格:例如:int startWord=box.SelectionStart;while box.Text[startWord]!=''{startWord-;}效果很好!除非用户将光标留在单词末尾。当尝试查找firstPos时,它会从子字符串抛出ArgumentOutOfRangeException。是否有可能在jQuery中偶然执行此操作?:
//this is our article
string article = " " + richTextBox1.Text.ToLower() + " ";
//we search the word from textbox1
int middle = article.IndexOf(textBox1.Text);
int headOfWord = article.LastIndexOf(" ", middle);
int tailOfWord = article.IndexOf(" ", middle);
//we have found the head and tail of the word
textBox2.Text = article.Substring(headOfWord, tailOfWord - headOfWord);
richTextBox1.Focus();
richTextBox1.Select(headOfWord, tailOfWord - headOfWord - 1);