.NET Framework Bookmark and Share   
 index > .NET Base Class Library > TextBox and managing the double-click selection bounds
 

TextBox and managing the double-click selection bounds

Hi all,

how can I manage the selection within a TextBox when the user double-clicks in it?

I want to be able to break the selection extent (left aswell as right)on a defined (set of) characters not just the (seeming default) of whitespace.

Can anyone advise?

David Harrison_ch
Try something like this:

private void textBox1_DoubleClick(object sender, EventArgs e) {
int start = textBox1.Text.LastIndexOf(' ', textBox1.SelectionStart);
int end = textBox1.Text.IndexOf(' ', textBox1.SelectionStart);
if (end < 0) end = textBox1.Text.Length + 1;
if (end > start) {
textBox1.SelectionStart = start + 1;
textBox1.SelectionLength = end - start - 1;
}

nobugz
Try something like this:

private void textBox1_DoubleClick(object sender, EventArgs e) {
int start = textBox1.Text.LastIndexOf(' ', textBox1.SelectionStart);
int end = textBox1.Text.IndexOf(' ', textBox1.SelectionStart);
if (end < 0) end = textBox1.Text.Length + 1;
if (end > start) {
textBox1.SelectionStart = start + 1;
textBox1.SelectionLength = end - start - 1;
}

nobugz

Hi Hans,

Many thanks for your reply...

I have tried the Double-Click event handler route.

If the TextBox is not selected when (say) the form is first displayed, there is no insertion point when we invoke (...).SelectionStart().

The Double-Click does not appear to cause an insertion to be made. If I see text in the TextBox like:

"aaa.b bb;ccc-ddd; eee"

and I want to break on the ';' chars and the user double clicks on the 'ccc', '-' or 'ddd' I would expect to be able to retrieve a string of "ccc-ddd". Without an insertion point there is no obvious way to go.

Have I missed something?

Thanks for your assistance

David Harrison

David Harrison_ch
The first click of the double-click sequence will put the caret (and SelectionStart) in the right spot. Did you try it?
nobugz

You can use google to search for other answers

Custom Search

More Threads

• error: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
• Least common denominator
• Creating ASP Webpages / VS2003.Net
• Where to store Data???
• Which is more efficient? try-catch or test for null
• How do I find who created a file
• Reg XML Node Comparison
• Create Comma Seperated Text File with C#
• ExecWB for print
• Displaying an X509Certificate2 that is not installed