Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to set focus on the textbox on a dialog form using C#

Tags:

c#

winforms

I have a textbox on dialog form, I want to default focus on it once form load, but for some reason, it does not happen. I have tried: Focus and form_load event and put focus method right after initialization of form.

like image 377
ikel Avatar asked Nov 16 '11 03:11

ikel


2 Answers

You may call Control.Focus() method in Dialog Form's Activated event handler or set Tab Index order or use Select() method.

like image 187
KV Prajapati Avatar answered Oct 12 '22 22:10

KV Prajapati


Use Form Shown event instead Load. Control cannot get focus while form doesn't shown.

public partial class Form1 : Form
{
   private void Form1_Shown(object sender, EventArgs e)
        {
              textBox1.Focus();
        }
}
like image 36
Victor Chekalin Avatar answered Oct 12 '22 23:10

Victor Chekalin