
Button.Focus() method doesn't work (sometimes)
SCENARIO:
I have a window used for time critical data entry that has several input
fields and buttons. The users want the [ENTER] key to jump them to the next
logical field which may not be the next control in the tab order depending
on the data they entered.
MY SOLUTION:
I trap the [ENTER] key in the Form_KeyPress event then depending on the
field that has focus and the data that has been entered I set focus using
the Focus() method of the control I want the focus to move to.
Example:
If TextBoxId.Text.Equals("") Then
TextBoxName.Focus()
Else
ButtonSave.Focus()
End If
PROBLEM #1:
This works great until I have to set focus to a button that was disabled
when the window first loaded (The button control's enable property was set
to False in the window designer so it would start off disabled). I can
successfully TAB to the button but writing code such as ButtonSave.Focus()
just jumps to the next control in the tab order after the ButtonSave control
that I actually wanted to receive focus. If I enable the buttons in design
mode the code runs great, but that leads to a different problem (See PROBLEM
#2).
PROBLEM #2:
If the buttons are enabled in design mode and then I disable them in the
Form_Load event they are not visibly disabled on the window. I tried to use
the Refresh() method after I disabled the buttons but they are still not
disabled.
This same basic design worked great in VB 6. Is my logic flawed, is there
an easier way of doing this in VB.Net????