【问题标题】:How to know current position of caret in TextBox(TextBlock) to select a certain word programmatically如何知道TextBox(TextBlock)中插入符号的当前位置以编程方式选择某个单词
【发布时间】:2016-01-17 05:57:26
【问题描述】:

例如,在 TextBox(TextBlock) C# WPF 中键入时

美丽的自然

beautiful_(这里,在输入nature之前,我想知道插入符号当前位置的索引。假设'_'下划线现在正在闪烁插入符号)

我要实现的是通过按 LeftShift 按钮设置选择的起始位置,并通过按 RightShift 按钮设置结束位置 这样我就可以仅在 textBox 中以编程方式选择最新(最近)的单个单词,以便在某处使用。

我一直在尝试使用以下简单代码的几种方法,但都失败了,并且在互联网上没有与我类似的案例..

或者作为更好的解决方案,有没有人知道一种方法,在完全输入美丽的自然之后,只按一次按键,以编程方式选择最近的单词“自然”?

如果有人为此分享卓越,我将不胜感激。

int startinglocation;
int endinglocation;
int selectionlength;


private void textBox_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.Key==Key.LeftShift) After typing Beautiful in textBox,
            {
            
                // To know the current location of Caret, Some wise instruction is needed here

            }
            if (e.Key == Key.RightShift) // After typing Nature in textBox,
            {


                // To know the current location of Caret, Some wise instruction is needed here

                int selectionlength=endinglocation- startinglocation;                    
                textBox.Select(startinglocation, selectionlength);
            }
        
    }

已解决

后者是 Nicolas Tyler 仅通过按一个键按钮来选择最近的最新单词的更好解决方案。谢谢泰勒先生。

private void textBox1_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.Key == Key.RightShift)
        {
            selectWord();
        }
    }
    private void selectWord()
    {
        int cursorPosition = textBox1.SelectionStart;
        int nextSpace = textBox1.Text.IndexOf(' ', cursorPosition);
        int selectionStart = 0;
        string trimmedString = string.Empty;
        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;
    }

【问题讨论】:

    标签: c# wpf textbox location caret


    【解决方案1】:

    使用

    TextBox.SelectionStart
    

    当您使用 WinForms 时,它会为您提供当前光标位置。

    当您使用 Presentation Foundation 时,您可以使用

    TextBox.CaretIndex
    

    如 MSDN TextBox.CaretIndex 中所述。

    【讨论】:

    • 你好,风暴。我非常欣赏你的想法。但是正如我已经尝试过的那样,在我的研究中,SelectionStart 仅在我们进行文本选择后指示所选文本的起始索引。我希望通过了解插入符号的当前位置以编程方式进行选择。现在,我已经发现 textBox.Select(textBox.Text(beautiful).Length, textBox.Text(beautiful nature).Length- textBox.Text(beautiful).Length) 我需要试试这个..利用2个长度的差异..跨度>
    • 当我在 WinForm 上创建一个文本框并在单击按钮时执行 MessageBox.Show(textBox1.SelectionStart.ToString()); 时,在文本框中写一个文本并将光标定位在例如第 5 个字符之后,然后我得到 5 返回!这行得通!当您使用 Presentation Foundation 时,您可以使用 TextBox.CaretIndex。这也有效。我已经更新了我的答案。
    • 亲爱的 Storm,我衷心感谢您对匿名人士的积极支持。但是,幸运的是,我发现后者提到了 Nicolas Tyler 提到的更好的解决方案,并且它成功地工作了。请参考我的更新。 stackoverflow.com/questions/5401068/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-25
    • 2012-09-25
    相关资源
    最近更新 更多