C#|如何通过光标位置在TextBox中选择单词?

时间:2009-09-18 02:48:27

标签: c# winforms textbox cursor

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

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

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

提前致谢!

5 个答案:

答案 0 :(得分:4)

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

这也可行

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;

答案 1 :(得分:2)

使用SelectionStart和SelectionLength属性 google

答案 2 :(得分: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);

答案 3 :(得分: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和此代码所在的按钮。 如果您需要任何帮助,请告诉我。

编辑:更改了一些代码并测试了所有方案。希望它有所帮助!

答案 4 :(得分: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)