1

When I use a C# TextBox as an input box, I found it hard to quickly select a word by doubleclicking on then text, because it sometimes selects brackets and other characters. For example, when I double click on var3, it selects enter image description here and what I want is enter image description here.

I tried adding doubleclick callback, but it seems be handled after default handler, and I don't know where user actually clicked, this also messes up the dragging operation after doubleclick to select multiple words.

Is there a easy way to redefine the separator chars for word selection?

  • Please add a tag specifying the UI framework you use (WPF, WinForms, Xamarin, UWP, MAUI, Unity, ...). And how is this related to Windows dev center? – Klaus Gütter Jun 02 '23 at 09:03

2 Answers2

2

You can handle the DoubleClick event and modify the SelectionStart and SelectionLength properties accordingly. Trim the delimiters at the start and end:

private static readonly char[] Delimiters = { ' ', '(', ')', ',', ';', '.', '-', ':', '[', ']' }; // and so on...

private void textBox1_DoubleClick(object sender, EventArgs e)
{
    TextBox txt = (TextBox)sender;
    string selectedText = txt.SelectedText;
    int selectStartOld = txt.SelectionStart;
    int selectLengthOld = txt.SelectionLength;
    int selectStartNew = selectStartOld;
    int selectLengthNew = selectLengthOld;
    bool inText = false;

    for (int i = 0; i < selectedText.Length; i++)
    {
        if (!inText && Delimiters.Contains(selectedText[i]))
        {
            // TrimStart
            selectStartNew++;
            selectLengthNew--;
        }
        else if (inText && Delimiters.Contains(selectedText[i]))
        {
            // TrimEnd
            selectLengthNew--;
        }
        else
        {
            inText = true;
        }
    }

    txt.SelectionStart = selectStartNew;
    txt.SelectionLength = selectLengthNew;
} 
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
0

My answer is similar to Tim's but I just focus on StartsWith and EndsWith.
So you still need to handle the doubleclick event of the button.

//Define as required
private static readonly string[] operators = { "(", ")", "[", "]", "{", "}", ",", ", ", ".", ", " }; 

private void textbox1_DoubleClick(object sender, EventArgs e)
{        
    if (operators.Any(x => textbox1.SelectedText.EndsWith(x)))
    {
        textbox1.SelectionStart = textbox1.SelectionStart;
        textbox1.SelectionLength = textbox1.SelectionLength - 1;
    }
    else if (operators.Any(x => textbox1.SelectedText.StartsWith(x)))
    {
        textbox1.SelectionStart = textbox1.SelectionStart + 1;
        textbox1.SelectionLength = textbox1.SelectionLength - 1;
    }
}
jason.kaisersmith
  • 8,712
  • 3
  • 29
  • 51
  • You will just remove one and not all operators. For example: `print(var1, var2, (var3))`. Here your result is `var3)` instead of `var3`. That's why i have a loop [here](https://stackoverflow.com/a/76388739/284240). You also don't handle that you can have operators on both sides, the `if...else` is broken, it could be both. – Tim Schmelter Jun 02 '23 at 11:36