0

在使用 C# 的 Windows 窗体中,如何根据光标位置选择(如实际突出显示文本,使其可供 .SelectedText 属性访问)一个单词?

这就是我想要做的。我有一个文本框,用户当前可以通过突出显示一个单词来选择它。然后他们可以对这个词执行各种操作,但我想让它更简单。

我希望这样做,以便他们可以简单地将光标放在单词中,然后应用程序将选择光标所在的单词。

提前致谢!

4

5 回答 5

5

可以使用SelectionStartandSelectionLength但你可能需要从光标位置找到下一个空格,然后反转文本框的内容,从“改变的光标”位置找到下一个“空格”,然后使用上面的两种方法。

这也将起作用

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;
于 2009-09-18T02:58:16.033 回答
2

使用 SelectionStart 和 SelectionLength 属性。
谷歌

于 2009-09-18T02:53:00.507 回答
1
    //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);
于 2016-06-14T22:35:37.780 回答
0

希望这可以帮助:

            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 和一个用于存放此代码的按钮。如果您需要任何帮助,请告诉我。

编辑:更改了一些代码并测试了所有场景。希望能帮助到你!

于 2009-09-18T03:10:09.427 回答
0

实际上我在这里有一个更简单的方法

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)
于 2011-12-12T08:44:18.767 回答