【问题标题】:Double click selecting words separated by periods like "i.a."双击选择用句点分隔的单词,如“i.a.”
【发布时间】:2021-11-16 16:28:49
【问题描述】:

我的目标是双击“i.a.”的整个部分like this 但是 WPF 的默认值是 selects each part of the string individually

示例:

<TextBox>Reason for contact: i.a.</TextBox>

到目前为止我发现了什么

我尝试使用SpellCheck.CustomDictionaries,单词选择不受这些影响。

【问题讨论】:

  • 有些混乱。您是否正在使用TextBoxRichTextBox 寻找此功能?是否需要文本格式?是否需要使用具有不同字体、颜色等的内容?
  • @Jackdaw 我正在查看两者,但我认识到 RichTextBox 与具有 TextPointer 的 TextBox 有很大不同,因此我将很快创建一个专门针对 RichTextBox 的新问题并编辑此问题

标签: wpf textbox


【解决方案1】:

试试这个。它根据插入符号的位置(即SelectionStart - 1)从最近的左侧空白' '(或字符串的开头)到最近的右侧(或字符串的结尾)选择文本部分。

private void OnMouseDoubleClick(object sender, MouseButtonEventArgs e)
{
    if (sender is TextBox tb)
    {
        char[] chars = tb.Text.ToCharArray();

        int i;
        // Find nearest left whitespace or start of string
        for (i = tb.SelectionStart - 1; i >= 0 && chars[i] != ' '; i--);
        int selectionStart = i + 1;

        // Find nearest right whitespace or end of string
        for (i = tb.SelectionStart; i < chars.Length && chars[i] != ' '; i++);
        int selectionLength = i - selectionStart;
        
        tb.Select(selectionStart, selectionLength);
    }
}

【讨论】:

    猜你喜欢
    • 2021-12-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-11
    • 2020-05-18
    • 2012-11-08
    • 1970-01-01
    • 2015-01-16
    相关资源
    最近更新 更多