Set focus on modal form

J

Janez

Hi,

I have designed application with two forms. First form serves as main
form, the second form has several groupboxes and is always displayed
modally with corresponding groupbox visible(depends on which button
was clicked in the main form). When the form is displayed, the focus
is also set to the first control in the visible groupbox (focus is set
in the Activate event of the form that is displayed modally). But, I
have found that this approach is not good, because focus is also set
when application receives focus and the form is displayed in the
application.
For example, if user displays second form, focus is set to the first
control in the visible groupbox. If now user sets focus to the third
control, switches to other application and then back, the focus is set
to the first control again which is what I do not want (I just want to
set focus when form is shown from application). So, the Activate event
of the form is not good for this action, but I cannot find the way to
set focus only when displaying form. I have tried Load event, but
focus does not work.

Any idea?

Regards
Janez
 
M

Mark Broadbent

To do this kind of thing I found a great way to get around this problem
(because a lot of the time certain events might fire multiple times because
of certain other events -which means a slight unpredictability when you want
to do something predictable).

My method is this:-

Dynamically assign a delegate method to the various event to do what you
want in code (for instance from the base application startup form load event
handler). At the end of the delegated method (in it's code) it can unassign
itself from the event delegate. This therefore means that the method would
fire once only and not multiple times.

Hope this helps.

Br,

Mark.
 
J

Jeffrey Tan[MSFT]

Hi Janez,

In Winform, by default, the first focused control is the child control that
has the smallest tabindex property. And normally, the first contol we
droped to the designer will have the smallest tabindex of 0, so this
control will have focus when form is shown out.

For your issue, we may just change the TabIndex of the control you want to
have focus to 0, and change the control which tabindex is 0 to other value.
Then when we open the form, the focus will on the control we set with
TabIndex to 0. I think this should meet you need.
===========================================
Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by to be
of assistance.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
J

Janez

Thanks Jeffrey,

setting tabindex solved my problem. Tabindexes of the controls in each
of my groupboxes were already set as should, but it didn't work.
After your post, I have checked all my tabindexes and found out that
it is also necessary to set tabindex of the visible groupbox to 0 (as
you may recall I have several groupboxes on the modal form, but only
one is visible at a certain time, depending on which button was
clicked in the main form).

Thanks again
Janez
 
J

Jeffrey Tan[MSFT]

Hi Janez,

Yes, if your controls are contained in groupbox, the setting for
Groupbox.TabIndex is needed. If you need further help, please feel free to
feedback. Thanks

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top