C# |如何通过光标位置选择文本框中的单词?

发布于 2024-08-05 16:17:15 字数 215 浏览 1 评论 0原文

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

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

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

提前致谢!

In a Windows Form, using C#, how do I select (as in, actually highlight the text, making it accessible to the .SelectedText property) a word based on the cursor location?

Here's what I'm trying to do. I have a textbox that users can currently select a word by highlighting it. They can then perform various actions to the word, but I want to make it simpler.

I wish to make it so they can simple put the cursor inside the word and the app will select the word the cursor is inside of.

Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

所谓喜欢 2024-08-12 16:17:15

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

You can use SelectionStart and SelectionLength but you probably need to find the next space from the cursor position, then reverse the contents of the textbox and find the next "space" from the "altered cursor" position, then use the two methods above.

This will also work

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;
寂寞陪衬 2024-08-12 16:17:15

使用 SelectionStart 和 SelectionLength 属性。
google

Use SelectionStart and SelectionLength properties.
google

糖粟与秋泊 2024-08-12 16:17:15
    //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);
    //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);
树深时见影 2024-08-12 16:17:15

希望这会有所帮助:

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

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

Hope this helps:

            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();

For this example, you need a text box, textBox1 and a button where this code goes.
Let me know if you need any help.

EDIT: Changed a bit the code and tested all the scenarios. Hope it helps!

冬天旳寂寞 2024-08-12 16:17:15

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

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)

actually i got a simpler approach here

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文