1

I have multiple text boxes in my windows form whose text I want to become selected when they are clicked on by the user. I am trying to find a way to make only the first click in a box select text when the user makes consecutive clicks in the box. I would like to have subsequent clicks in the same box position a text cursor box rather than endlessly select the text, which is what is happening now.

I have tried calling focused.Select() inside an if statement that only runs if no text is already selected like shown below. The problem with that is that right after a user clicks on selected text briefly "un-selects" then Highlight_OnClick runs and the text is again selected.

    private void HighlightWhenFocused(object sender, EventArgs e)
    {

        if (sender is TextBox)
        {
            TextBox focused = sender as TextBox;
            focused.Select(0, focused.Text.Length);
            //if (focused.SelectionLength == 0 { focused.Select(0, focused.Text.Length); }    
            // does not work either
        }
    }

My code above always selects the text but the problem is that even once the user has clicked on a text box that is already selected, subsequent clicks only re-select the text.

Josh
  • 76
  • 4

1 Answers1

2

You sound like you have bound this event handler to the Click event on the Textboxes. You should use the Enter event instead. This will fire once when the control is focused, but subsequent clicks will not re-trigger:

enter image description here

pcdev
  • 2,852
  • 2
  • 23
  • 39
  • this event is actually bound to the Enter event as well, this allows me to use Tab to select the boxes, clicking on a textbox does not focus it and even if I changed that I would still have the same problem. Thanks for the suggestion! – Josh Aug 05 '19 at 04:19