0

I need to have the text in a TextBox become selected when a user clicks into the box. If the text is already selected, it needs to be a regular cursor. So on the click event of all the textboxes I have this code:

TextBox t = (TextBox)sender;
bool alreadyselected = t.SelectedText == t.Text;
if (!alreadyselected) t.SelectAll();

the problem is, by the time the click event is reached, t.SelectedText is empty so the full text always becomes selected even when clicking multiple times

I would appreciate a solution that can be for all the textboxes at once if possible

Jill
  • 473
  • 3
  • 7
  • 15
  • Have you tried registering to `TextBox.Enter` instead? Its the first raised event when a `System.Windows.Forms.Control` is selected by any means. – Ivan García Topete Aug 23 '18 at 17:08
  • 1
    Or if `t.SelectedText == string.Empty` then you can always call `t.SelectAll()` right? (removing the `if` condition) Also, you can just change your conditionals with `if (t.SelectedText != t.Text) t.SelectAll();` – Ivan García Topete Aug 23 '18 at 17:12
  • @IvanGarcíaTopete the enter event does not work for selecting text; text always subsequently becomes unselected. – Jill Aug 23 '18 at 17:32
  • @IvanGarcíaTopete SelectedText is showing empty when there was text selected. I think calling the click even de-selects it. Thats why my method doesnt work – Jill Aug 23 '18 at 17:33
  • Then in your `Click` event remove the `if` conditional, always call `t.SelectAll();` – Ivan García Topete Aug 23 '18 at 18:28
  • @IvanGarcíaTopete I want it to select all when a user first clicks into the box, but clicking again should deselect and become a regular cursor – Jill Aug 23 '18 at 19:00
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/178636/discussion-between-ivan-garcia-topete-and-jill). – Ivan García Topete Aug 23 '18 at 19:07
  • Possible duplicate of [Making a WinForms TextBox behave like your browser's address bar](https://stackoverflow.com/questions/97459/making-a-winforms-textbox-behave-like-your-browsers-address-bar) – Ivan García Topete Aug 24 '18 at 14:08

2 Answers2

0

You're correct, the default Click for the TextBox is changing the position of the caret and thus clearing any selected text. But you can restore it.

First add 2 int vars to store the selection Start and Length and initialize Start as -1 to signal not set:

private int SelectedStart = -1;
private int SelectedLength = 0;

then make a handler for the TextBox's Leave event and save the Start and Length for the currently selected text when we lose focus.

private void textBox1_Leave (object sender, EventArgs e)
{
    SelectedStart = textBox1.SelectionStart;
    SelectedLength = textBox1.SelectionLength;
}

Finally, make a handler for the TextBox's Click event and, if we previously saved the Start and Length, restore them to the TextBox and then set Start to -1 to signal not set again (this allows for normal click behavior within textbox when it is focused).

private void textBox1_Click (object sender, EventArgs e)
{
    if (SelectedStart != -1) {
        textBox1.SelectionStart = SelectedStart;
        textBox1.SelectionLength = SelectedLength;
        SelectedStart = -1;
    }
}
Jeff R.
  • 1,493
  • 1
  • 10
  • 14
0

Use the Control.Tag property to set a bool flag to select or deselect the TextBox text:

private void TextBox_Click(object sender, EventArgs e)
{
    TextBox txtBox = (TextBox)sender;
    txtBox.SelectionStart = 0;
    // First click will select the text
    if (txtBox.Tag == null)
    {
        txtBox.Tag = true;
        txtBox.SelectionLength = txtBox.Text.Length;
    }
    // Second click will deselect the text
    else
    {
        txtBox.Tag = null;
        txtBox.SelectionLength = 0;
    }
}