Search code examples
c#winformstextbox

Textbox's text is not selected when launching the exe directly


I have a simple textbox with a text saying "Username".

  • When I launch the program in Visual Studio, the whole text "Username" is selected so when i start typing it will be ok, since it overwrites it.
  • But when i launch the exe directly, the username is not selected, the cursor pointer is before the first letter, so if i type "Anything" it will be "AnythingUsername".

Picture what i'm talking about: enter image description here

It must be some bug, Is it possible to make the "Username" default text selected when i launch the exe directly?


Solution

  • You can select the text form inside the text box when it loads

    private void Form_Load(object sender, EventArgs e)
    {
        textBox1.SelectionStart = 0;
        textBox1.SelectionLength = textBox1.Text.Length;
    }