Sunday, November 15, 2009

Setting Focus to a Control at Form Load

I used to write a “.Net Tips and Tricks” column for an online magazine associated with a forum called the  Universal Thread. The magazine has been known by several names, the Universal Thread Magazine, Level Extreme .NET Magazine and simply the UT Mag. Unfortunately, after being in “publication” since 2001, the last issue was in April of this year and it doesn’t look like it will be published anymore.

So, if no one minds, I think it’s time to start including a few of the Tips from my columns over the years. The format of my column was to garner tidbits of wisdom from other people’s posts on the UT, re-work them into a good format for a Tip, and credit the people who wrote the post (sometime it was even from a post that I wrote).

So, let’s start out with this --- how about a simple WinForms tip. Nothing earth-shattering here, but worth mentioning.

Normally, the Focus() method of a control is used to move the focus to that control:

private void MyButton_Click(object sender, System.EventArgs e)
// Move the Focus elsewhere

This works at any time, except during Form/Control Load. Then, you simply have to set the ActiveControl:

private void MyForm_Load(object sender, System.EventArgs e)
// Start the Form with the Focus on a certain control
this.ActiveControl = this.MySpecialTextBox;

Thanks to Kevin McNeish in Message #1080245 on the Universal Thread.


  1. I love this blog. If I knew what you were talking about, this blog would be sooooo helpful, I just know it!! But I love it anyway. I definately didn't get the "geek" gene. Are you sure we're related? Did you see the meteor shower last night? (Stay tuned to MY blog for more on that!) I hope you don't mind me posting a comment here - I figured since I'm your little sister and all, that it would be all right. I'm just very impressed! Really - I am!

  2. Hey Sheri! Glad you like my blog, even if you don't understand any of it!! Nope, you did not get the "geek" gene ... but, didn't we always tell you that you were adopted? ;0)

    Did not see the meteor shower (I hear it was pretty sweet). I'll read about it on your blog once you get it posted.