How to set focus to a text box on windows form load?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Walter

    How to set focus to a text box on windows form load?

    I want to set focus to a text box on a C# windows form when the form loads.
    How can I do this? I want the cursor to be in this text box when the form
    loads so the user can just start typing. Below is what I have so far:

    private void County_Load(obj ect sender, EventArgs e)
    {
    txtCounty.Focus ();
    }

    Thanks for the help

  • Mel Weaver

    #2
    Re: How to set focus to a text box on windows form load?

    Do it in the show event

    "Walter" <waltmallon@yah oo.comwrote in message
    news:AD17B415-A2A0-456A-9768-5A25FA477F59@mi crosoft.com...
    >I want to set focus to a text box on a C# windows form when the form loads.
    >How can I do this? I want the cursor to be in this text box when the form
    >loads so the user can just start typing. Below is what I have so far:
    >
    private void County_Load(obj ect sender, EventArgs e)
    {
    txtCounty.Focus ();
    }
    >
    Thanks for the help

    Comment

    • Walter

      #3
      Re: How to set focus to a text box on windows form load?


      Bear with me as I am new to C# (been a DBA for 10 years). How would I do
      that in the show event? Thanks.


      "Mel Weaver" <MelRemoveSpam@ Insdirect.comwr ote in message
      news:ucyH%23fsO JHA.4180@TK2MSF TNGP05.phx.gbl. ..
      Do it in the show event
      >
      "Walter" <waltmallon@yah oo.comwrote in message
      news:AD17B415-A2A0-456A-9768-5A25FA477F59@mi crosoft.com...
      >>I want to set focus to a text box on a C# windows form when the form
      >>loads. How can I do this? I want the cursor to be in this text box when
      >>the form loads so the user can just start typing. Below is what I have so
      >>far:
      >>
      > private void County_Load(obj ect sender, EventArgs e)
      > {
      > txtCounty.Focus ();
      > }
      >>
      >Thanks for the help
      >
      >

      Comment

      • Peter Duniho

        #4
        Re: How to set focus to a text box on windows form load?

        On Thu, 30 Oct 2008 12:50:03 -0700, Walter <waltmallon@yah oo.comwrote:
        Bear with me as I am new to C# (been a DBA for 10 years). How would I
        do that in the show event? Thanks.
        The same way you've already done it for the Load event, except attach your
        handler to the Show event instead.

        That said, an alternative would be simply to set the TabIndex for the
        TextBox to be the lowest TabIndex for the Form (typically, that would be
        "0"). Then the framework will simply set the focus to that TextBox
        automatically for you.

        Pete

        Comment

        • Peter Duniho

          #5
          Re: How to set focus to a text box on windows form load?

          On Thu, 30 Oct 2008 13:10:21 -0700, Peter Duniho
          <NpOeStPeAdM@nn owslpianmk.comw rote:
          On Thu, 30 Oct 2008 12:50:03 -0700, Walter <waltmallon@yah oo.comwrote:
          >
          >Bear with me as I am new to C# (been a DBA for 10 years). How would I
          >do that in the show event? Thanks.
          >
          The same way you've already done it for the Load event, except attach
          your handler to the Show event instead.
          Er, make that the "Shown" event. Intellisense should make it easy to
          figure that out, but just in case... :)

          Comment

          Working...