TextBox Focus Problem

  • Thread starter Thread starter Beebs
  • Start date Start date
B

Beebs

How come when calling the following code from a button, even with the
cursor in txtFraction, it never evaluates to True? This function, no
matter where the cursor is always says txtNumber has the focus. Can
anyone see what I'm doing wrong?

Private Sub Button_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs)
Dim btn As Button = CType(sender, Button)

If txtNumber.Focus = True Then
If btn.Text = "Fraction" Then
txtFraction.Focus()
Else
txtNumber.Text = txtNumber.Text & btn.Text
End If
ElseIf txtFraction.Focus = True Then
If btn.Text = "Fraction" Then
txtNumber.Focus()
Else
txtFraction.Text = txtFraction.Text & btn.Text
End If
End If
End Sub
 
When you click on the button, then the button has the focus and none of the
text boxes.
 
Then why does the cursor move to between text boxes like I want but
then when I press another button to enter a number it goes back to the
txtNumber textbox, that's what makes no logical sense to me...
 
Put a breakpoint on every statement in your code that says "txtNumber.Focus
= True" and run it in the debugger and see what happens.
 
Not sure what you mean by "see what happens", but I can tell you from
debugging this routine before that txtFraction.text never evaluates to
true but txtNumber always does, even if the cursor is in
txtFraction...like I said earlier, it doesn't make sense.
 
Whenever you click a button, the button gains focus. There's no way around
that. So regardless of where the cursor is, Focus moves to the button when
it's clicked and your function will always be False.

-Chris
 
Back
Top