Dynamically placing focus on a specific control at startup

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

    Dynamically placing focus on a specific control at startup

    Hi Gang,

    I have two text boxes on a form and if the first text box is
    populated, I want to place focus on the second text box at startup.
    However, this never seems to work. What am I doing wrong here?

    private void frmMain_Activat e(object sender, EventArgs e)
    {
    if (txtOne.Text != "")
    {
    this.txtTwo.Foc us();
    }
    }

    private void InitializeCompo nent()
    {
    this.txtOne = new System.Windows. Forms.TextBox() ;
    this.txtTwo = new System.Windows. Forms.TextBox() ;
    this.SuspendLay out();
    //
    // txtOne
    //
    this.txtOne.Loc ation = new System.Drawing. Point(12, 70);
    this.txtOne.Nam e = "txtOne";
    this.txtOne.Siz e = new System.Drawing. Size(100, 20);
    this.txtOne.Tab Index = 0;
    this.txtOne.Tex t = "I\'m full!";
    //
    // txtTwo
    //
    this.txtTwo.Loc ation = new System.Drawing. Point(180, 70);
    this.txtTwo.Nam e = "txtTwo";
    this.txtTwo.Siz e = new System.Drawing. Size(100, 20);
    this.txtTwo.Tab Index = 1;
    //
    // frmMain
    //
    this.AutoScaleD imensions = new System.Drawing. SizeF(6F,
    13F);
    this.AutoScaleM ode =
    System.Windows. Forms.AutoScale Mode.Font;
    this.ClientSize = new System.Drawing. Size(292, 273);
    this.Controls.A dd(this.txtTwo) ;
    this.Controls.A dd(this.txtOne) ;
    this.Name = "frmMain";
    this.Text = "Form1";
    this.Load += new
    System.EventHan dler(this.frmMa in_Activate);
    this.ResumeLayo ut(false);
    this.PerformLay out();

    }

    Thanks,
    Christian
  • Alberto Poblacion

    #2
    Re: Dynamically placing focus on a specific control at startup

    "Christian Blackburn" <christian.Blac kburn@yahoo.com wrote in message
    news:0b196b45-e1c6-4436-b01b-d0e996ea4df2@26 g2000hsk.google groups.com...
    I have two text boxes on a form and if the first text box is
    populated, I want to place focus on the second text box at startup.
    However, this never seems to work. What am I doing wrong here?
    >
    private void frmMain_Activat e(object sender, EventArgs e)
    {
    if (txtOne.Text != "")
    {
    this.txtTwo.Foc us();
    }
    }
    [...]
    this.Load += new
    System.EventHan dler(this.frmMa in_Activate);
    Look at the last line that I quoted. You are connecting the routine that you
    named "frmMain_Activa te" to the LOAD event of the form (instead of Activate,
    as the name seems to imply). At the time of the Load event the form is not
    yet visible, so the Focus() method doesn't work.

    Comment

    • Christian Blackburn

      #3
      Re: Dynamically placing focus on a specific control at startup

      On Jul 13, 11:43 pm, "Alberto Poblacion" <earthling-
      quitaestoparaco ntes...@poblaci on.orgwrote:
      "Christian Blackburn" <christian.Blac kb...@yahoo.com wrote in message
      >
      news:0b196b45-e1c6-4436-b01b-d0e996ea4df2@26 g2000hsk.google groups.com...
      >
      I have two text boxes on a form and if the first text box is
      populated, I want to place focus on the second text box at startup.
      However, this never seems to work.  What am I doing wrong here?
      >
             private void frmMain_Activat e(object sender, EventArgs e)
             {
                 if (txtOne.Text != "")
                 {
                     this.txtTwo.Foc us();
                 }
             }
      [...]
                 this.Load += new
      System.EventHan dler(this.frmMa in_Activate);
      >
      Look at the last line that I quoted. You are connecting the routine that you
      named "frmMain_Activa te" to the LOAD event of the form (instead of Activate,
      as the name seems to imply). At the time of the Load event the form is not
      yet visible, so the Focus() method doesn't work.
      Hi Alberto,

      I tried to this.Active += new; and that didn't work, so I used
      this.Load which I wasn't sure whether that was the name of the event
      or the action of loading that event handler. This is the compiler
      error I get when I try using activate:
      Cannot assign to 'Activate' because it is a 'method group'

      Do you think I am declaring my event in the wrong place?

      Thanks,
      Christian

      Comment

      • Alberto Poblacion

        #4
        Re: Dynamically placing focus on a specific control at startup

        "Christian Blackburn" <christian.Blac kburn@yahoo.com wrote in message
        news:a7b25abe-cd78-4f8b-b409-0a8ce3b791c0@w7 g2000hsa.google groups.com...
        I tried to this.Active += new; and that didn't work,
        Should be "Activated" , not "Active".

        Anyway, when in doubt, you can use the designer in Visual Studio to
        connect the event for you: Click on the form in design view, and then go to
        the Properties window and locate an icon at the top of this window that
        looks like a lightning bolt. When you press it, the properties window
        displays the events instead of the properties. Here you can remove an event
        that you have connected and add a new event handler, or connect an existing
        one, to the event of your choice.

        Comment

        Working...