Link to home
Start Free TrialLog in
Avatar of jeremiahscott
jeremiahscott

asked on

Form onload and foucs issues

hi.

i have a form that i create when my app starts. then when a user right clicks i show it. the first time it pops up the first out of three buttons has focus like it should. each button has a tab index of 1,2,3 repsectively. also, in the form on load i have button.focus();. however, whenever i popup the form for the second+ time the last button that was clicked on is focused, not button one. i dont understand why the focus method isnt setting foucs to button one. how can i achieve this?

thank you.
Avatar of AlexFM
AlexFM

Handle VisibleChanged Event and set focus there:

private void Form_TextChanged(object sender, EventArgs e)
{
    if ( Visible == True )
        button.focus();
}
Are you using ShowDialog or Show ?

Basically it sounds like you are setting the focused item and then calling ShowDialog, this would appear to be setting the focus prior to display but when the Form displays it is restoring the form's state to the state when it was last closed.

What you could do is place the button.Focus() call into the form's (which has the buttons in) Activated event.

Hope this helps
Avatar of jeremiahscott

ASKER

im still a little confused.
i will have to check tonite if i am using show or show dialog.

what i do know is that from my main form i call show/showdialog. then i set a breakpoint in the onload of the form it is showing. it goes thru there and calls the button1.focus(). i thought that onload was called each time the form is shown, which is seems to be since the break point is hit. i still dont understand why calling button1.focus() doent work there. sorry =(
ASKER CERTIFIED SOLUTION
Avatar of purpleblob
purpleblob

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
thanks for the detailed explination. i understand what is going on now.
Glad to have helped :-)