How do I focus a TextBox
element on the main form after closing a second form which is opened by calling ShowDialog()
?
I have tried to call MainForm.TextBox.Focus()
in the closing and closed event of the second form, but this won't focus the textbox.
I am using System.Windows.Forms
on the compact framework.
Thanks.
ShowDialog() will only return when the second form is closed, so you can write
MyTextBox.Select()
right after the call.From your second form, make a button (or another control) return a
DialogResult
by going into Properties. When you want the second form to close (ie after you press a button) make it return a specificDialogResult
. In your main form you can do this:Calling
ShowDialog()
will block until it is closed so you could simply do:However the first example is for when you only want to make the textbox have focus after you press a certain button or do an action on the second form.
here you're showing the new form. When you close it, you will execute methods after that, so add
so, its:
ShowDialog means, it's a modal window and the focus won't go back to main form until you close second form. You can set focus back in the same code which you used to open the second form.