I have a form that loads, hits a button (to add text to a textbox), moves the cursor to the end of the textbox text, and then sets the active control to the textbox, so the user can immediately start typing. The textbox is populated fine, but the form loses its focus. This is only with the selection line in there, if I take it out, it works fine. The user has to click on the form to make it active. Any ideas?
private void createNewFolder_Load(object sender, EventArgs e)
{
addDate.PerformClick();
folderNameTextBox.Select(folderNameTextBox.Text.Length, 0);
this.ActiveControl = folderNameTextBox;
this.Focus();
}
Focusing cannot work in the Load event, the form is not yet visible. By far the simplest way is to just give the control the lowest TabIndex. Or use the Select() method: