Click here to Skip to main content
15,891,184 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

We are facing an issue with setting the default focus for a custom button control. I will try to explain the issue we are facing with a simple example below.

We had created two custom button controls - OK and Cancel on a form and had set the form's AcceptButton property to the OK button. Ideally this should set the default focus on the OK button on form load. But we are unable to see the default focus on OK button when we see the form in runtime. And we found that, only when the focus goes to another form, does the default focus appear on the OK button of the first form (default focus appears as a thick line around the button).

Could you kindly help us understand why the default focus does not appear on the OK button on form load? Do we have to do something additional to fix this?
Posted

1 solution

In Windows, "focus" means "the user can interact with this object by way of the keyboard." As such, one and ONLY one object can have the focus at a time. If FormB has the focus, it is impossible for a control on FormA to also have the focus.

So, if you need to keep a border around a given button, you will need to create a custom button that manually draws a border around the button. That is a bit beyond a quick answer, but it is reasonably simple and there are many examples to be found on the web.
 
Share this answer
 
Comments
Preeti.Ravindran 25-Jan-14 2:12am    
Thanks Gregory. I understand what you are saying.
Let me clarify a bit more.

Form A has an OK button which opens the Form B using the ShowDialog() method.
Now, the issue is that when the Form A has the focus, I don't see the focus visually on the OK button, in other words I don't see the thick line around the OK button. But I can see this thick line around the OK button (of Form A) when the button is clicked and the focus goes to Form B.

Our requirement is that the user should be able to see that the OK button has the focus when Form A loads at runtime. This is not happening.
Hope this explains our issue a bit more clearly.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900