The improved answer from DarkPh03n1X worked almost for me, however it has an unpleasant error: if no separator character is found , Text.IndexOf(c, start) will return -1 , which will set right to -1 , and then if (right == -1) right = Text.Length triggers.
So, now we have chosen to the end of the text, although the expected choice should be shorter. I think the launch is being processed correctly.
I removed if (right == -1) right = Text.Length , but added && pos != -1 . Here is the corrected version:
class RichTextBoxX : RichTextBox { // implement selection to work with "whole words" on double-click // and without selecting the leading/trailing spaces/blanks/line breaks private char[] delimiterList = new[] { '\n', ',', ' ', '(', ')', '_', '/' }; protected override void WndProc(ref Message m) { if (m.Msg == 0x0203) // WM_LBUTTONDBLCLK { int start = SelectionStart; if (start < 1) start = 1; int left = -1; int right = Text.Length; int pos; foreach (char c in delimiterList) { pos = Text.LastIndexOf(c, start - 1); if (pos > left) left = pos; pos = Text.IndexOf(c, start); if (pos < right && pos != -1) right = pos; } SelectionStart = left + 1; SelectionLength = right - left - 1; return; } base.WndProc(ref m); } }
To test the behavior, here is an example of the text I used:
12.34.56.78 (ab1-2-3-4-5.test-1.example.com) Jersey City New Jersey US, United States ASN: Example.com/12345
I added a few other separators, feel free to choose what you need.
Eugen source share