Sunday, May 18, 2008

.Net WinForms Control.Focus

Today I spent a good amount of time to set the focus for my newly created text box. Somehow the txtName.Focus() was returning false. After googelling for a while, I realized that the control was not added to parents collection which prevented it to gain the focus.

So, if you are creating the control by yourself remember to add it first to parents control collection and then call Control.Focus().

Also, visit http://msdn.microsoft.com/en-us/library/system.windows.forms.control.focus.aspx for more details on further requirements for setting control's focus.

0 comments: