【问题标题】:C# | How Do I Select a Word in a TextBox by Cursor Location?C# |如何通过光标位置在文本框中选择一个单词?
【发布时间】:2023-03-11 15:09:02
【问题描述】:

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

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

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

提前致谢!

【问题讨论】:

    标签: c# winforms textbox cursor


    【解决方案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);
    

    【讨论】:

      【解决方案2】:

      您可以使用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;
      

      【讨论】:

        【解决方案3】:

        其实我这里有一个更简单的方法

        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)
        

        【讨论】:

        • 有可能在 jQuery 中偶然做到这一点吗? :)
        【解决方案4】:

        希望这会有所帮助:

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

        编辑:更改了一点代码并测试了所有场景。希望对您有所帮助!

        【讨论】:

        • 效果很好!除非用户将光标留在单词的末尾。尝试查找 firstPos 时,它会从 Substring 中抛出 ArgumentOutOfRangeException。
        【解决方案5】:

        使用 SelectionStart 和 SelectionLength 属性。
        google

        【讨论】:

        • hmm... 我可以使用 SelectionStart 获取光标位置。如果他们将光标放在单词的中间会怎样(在我的情况下更有可能)。我不清楚如何实际选择这个词。 (我来自网络开发背景......)。提前致谢。
        • 使用 .Text 并找到空格:例如: int startWord = box.SelectionStart; while (box.Text[startWord] != ' ') { startWord--; }
        猜你喜欢
        • 1970-01-01
        • 2016-04-16
        • 2011-06-11
        • 2010-10-06
        • 1970-01-01
        • 2014-01-20
        • 1970-01-01
        • 1970-01-01
        • 2021-12-14
        相关资源
        最近更新 更多