Search code examples
c#winformstextboxlistboxcursor-position

Set the cursor on a textbox after selecting an item in a ListBox


When I select an item on my Listbox, a Textbox is filled with the selected text for editing. How can I get the cursor to focus on the Textbox text so I don't have to click on it with my mouse before editing?


Solution

  • Just use the Control's Focus method:

    TextBox1.Text = selectedItemText;    
    TextBox1.Focus();
    

    Or

    TextBox1.Select();
    

    Or if you just want to put a cursor after the last letter in the TextBox:

    TextBox1.Select(TextBox1.Text.Length, 0);
    TextBox1.Focus();