check textbox focus

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

    check textbox focus

    I have 2 textboxes and one button and I don't know why my code doesn't work.
    All I'm trying to do is write a number 1 into the textbox with the focus. so
    if the user puts the cursor in textBox1 and pushes the button the 1 should go
    into that box. If the cursor is in the other one the 1 should go there. It
    seems simple but doesn't work. Can anyone give me any hints? Here is the code:

    private void button1_Click(o bject sender, System.EventArg s e)
    {
    if (textBox1.Focus ed == true)
    textBox1.Text = "1";
    if (textBox2.Focus ed == true)
    textBox2.Text = "1";
    }
  • ALI RAZA

    #2
    Re: check textbox focus

    Salam

    Your code will not work correctly because when you click a Text Box, Focus
    event is not fired, and as a result focus is false.

    When you click on the TextBox, the Text Box Enter event is fired, here you
    can set the focus of the text box by calling its Focus() Function. In this
    way, ur code will work correctly


    --
    ALI RAZA SHAIKH
    MCAD.net


    alirazashaikh.b logspot.com


    "orahm" <orahm@discussi ons.microsoft.c om> wrote in message
    news:E04DDF90-9513-417C-98BE-42984A8AA7A5@mi crosoft.com...[color=blue]
    >I have 2 textboxes and one button and I don't know why my code doesn't
    >work.
    > All I'm trying to do is write a number 1 into the textbox with the focus.
    > so
    > if the user puts the cursor in textBox1 and pushes the button the 1 should
    > go
    > into that box. If the cursor is in the other one the 1 should go there. It
    > seems simple but doesn't work. Can anyone give me any hints? Here is the
    > code:
    >
    > private void button1_Click(o bject sender, System.EventArg s e)
    > {
    > if (textBox1.Focus ed == true)
    > textBox1.Text = "1";
    > if (textBox2.Focus ed == true)
    > textBox2.Text = "1";
    > }[/color]


    Comment

    • Christian Stueben

      #3
      Re: check textbox focus

      Hi Orahm,
      maybee i am wrong, but i would say that as soon as you klick on button1, the
      button has the focus, So no one of your textboxes can have focus at that
      moment.

      To verify, add to your code:
      if (button1.Focuse d == true) textbox1.Text = "b";
      and try again. What happens?

      greetings from germany
      Chris

      Comment

      • Christian Stueben

        #4
        Re: check textbox focus

        Hi Orahm,
        i forgot to say...
        you could use the .Leave-Event (is fired when focus leaves an element) to
        let the textboxes write their number to some little helper variable. So the
        button click can use the helper variable to determine which textbox had last
        the focus.

        Grettings from germany
        Chris

        Comment

        • orahm

          #5
          Re: check textbox focus

          Hi, thanks for replying. Both of your assumptions are correct. the focus is
          always false because the button has it. Which event should I use to write the
          one in the "last chosen" text box? I couldn't find an enter event?

          "Christian Stueben" wrote:
          [color=blue]
          > Hi Orahm,
          > maybee i am wrong, but i would say that as soon as you klick on button1, the
          > button has the focus, So no one of your textboxes can have focus at that
          > moment.
          >
          > To verify, add to your code:
          > if (button1.Focuse d == true) textbox1.Text = "b";
          > and try again. What happens?
          >
          > greetings from germany
          > Chris
          >
          >[/color]

          Comment

          • orahm

            #6
            RE: check textbox focus

            Thanks a lot the leave event did the trick

            "orahm" wrote:
            [color=blue]
            > I have 2 textboxes and one button and I don't know why my code doesn't work.
            > All I'm trying to do is write a number 1 into the textbox with the focus. so
            > if the user puts the cursor in textBox1 and pushes the button the 1 should go
            > into that box. If the cursor is in the other one the 1 should go there. It
            > seems simple but doesn't work. Can anyone give me any hints? Here is the code:
            >
            > private void button1_Click(o bject sender, System.EventArg s e)
            > {
            > if (textBox1.Focus ed == true)
            > textBox1.Text = "1";
            > if (textBox2.Focus ed == true)
            > textBox2.Text = "1";
            > }[/color]

            Comment

            Working...