How to prevent background winform application from stealing focus

Hi experts
My winform application consist of menu items and menu buttons.
While my application is running, I cover half of it by another program so that my application has lost its focus and goes to the background.

The problem is when my mouse hovers over the menu items and menu buttons of my (background) application, it would steal focus (by highlighting itself) even though it is still half-covered by another program.

I don't want my application to steal any focus while running on the background. I'd tried this in my main form but doesn't work:
protected override bool ShowWithoutActivation { get { return true; } }

Open in new window


Any idea please. Thanks in advance.
dominicwongAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jacques Bourgeois (James Burger)PresidentCommented:
First of all a form cannot have the focus unless its shows only controls that cannot have the focus. This would typically be a form that contains only Labels control. As soon as you have an enabled TextBox or a ComboBox or any enabled focusable control on the form, it's one of these controls that has the Focus, not the form.

Also, as a side information, the notion of Focus is often misunderstood. There is not one focus, but there are many. Each form has one focus, on its control that was last used.

Thus, most forms never get the Focus. They got Activated or Deactivated, but they don't get the focus. And you can have only one form activated at a time.

Hovering over a form should not steal the activated state from the currently activated form. The user has to click on a form or one of its controls to activate it and "steal" the activation from another one.

Are you sure that you do not react to some event such as MouseMove or MouseOver in your form?

What I would do is put a breakpoint in the Activated event of the form that causes the problem. When you hit the breakpoint, give a look at the Call Stack in the debugger. You will see what was called in the application just before the Activated event, and it should point to what causes the activation.
0
dominicwongAuthor Commented:
Thanks James Burger for your quick response.
I apologize that I didn't explain my issue clearly.

It wasn't the form of my application that had stolen the focus.
What I meant was the menu items and the menu buttons on my form that had stolen the focus when my mouse hovered over these controls while the form itself is on the background.
 (The form itself didn't steal the focus. These controls on the form did.)

What I want is that these controls will not steal any focus while the form is still in the background.

I've checked that I haven't got any handlers to MouseMove or MouseHover for these controls.

Thanks again.
0
ste5anSenior DeveloperCommented:
It's not clear what your problem is.

But the solution is simple: Just deactivate all controls on your form, when it gets deactivated (loses focus).  Enable them, when your form gets activated.
0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

Jacques Bourgeois (James Burger)PresidentCommented:
Sorry for my own late answer, I was out of town, in another environment, and somehow your second post did not reach me.

A Control cannot steal the focus from another contorl or deactivate another form unless you click on it, move to it with Tab or get activated through a shortcut.

Menu items and buttons in a Windows Form are highlighted when you mouse over them, a good way to show the user which one would be activated if he clicks. But they do not get the focus unless you click on them.

If what you mean by "steal the focus" is what it really means, then there is some code somewhere that does it.

You should not only give a look to the MouseHover or MouseMove of the menus themselves. To go to any control, you need to mouse over the form itself and any other control that is in the path of the mouse cursor. So MouseHover and MouseMove events from the form or another control could also be the culprit. And I told you about these 2 events because they are the most evident from your description of the problem. It can come from somewhere else.

Are there Select, Activate or Focus calls in your application? You might have a Timer that triggers the thing.

One thing is sure, you cannot steal the focus from a control or deactivate another form only by hovering over a control unless there is code that does it somewhere.
0
dominicwongAuthor Commented:
Thanks James for pointing out the difference between items "stealing focus" and "got highlighted".

My problem is the latter.  I noticed that items and buttons in different applications responded differently to mouse hovering. I did an experiment on (1) Microsoft Outlook and Microsoft VS2008, (2) Internet Explorer and Microsoft VS2008.

When the Microsoft Outlook had the focus, hovering mouse over the Microsoft VS2008 did not highlight any menu items or buttons on the Microsoft VS2008. (The only items got highlighted were the Minimize/Maximize/Close buttons at the top right hand corner of the Microsoft VS2008 ).

When the Microsoft VS2008 had the focus, hovering mouse over the Internet Explored highlighted every buttons on the Internet Explorer.

Is there any way to make menu items or buttons not to get highlighted to mouse hovering (like the Microsoft VS2008) when the application is not under focus?

Thank you.
0
Jacques Bourgeois (James Burger)PresidentCommented:
Different applications can use different controls to perform the same task. Outlook is a COM application, not a .NET application, so it uses older types of controls.

I just tested with 3 different browsers (Firefox, Internet Explorer and Microsoft Edge), and when you run them with multiple tabs, their tabs are also highlighted when the browser is in the background of another window. Visual Studio 2015 also does. Modern applications seem to do that. But Outlook uses the same old toolbars that were used in Office 2003.

So this depends on the application and the type of toolbars or tab control that you use. Some do, and some don't. I never heard anybody complaining about that. Most people do not even notice.

The only way to really prevent that would be to disabled the controls on the form as suggested by ste5an, but in my opinion, this would be even worse. No application does that. That would require you to work to make your form look and act in a weird way.

All things considered, what you want is to make your application behave like they did 12 years ago. Modern applications behave like the ToolStrip does in .NET.

It also make sense that the buttons are highlighted, as a way to show the use that he will get back to the application sitting in the background if he clicks.

So, personnally, I would leave it as it is.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
dominicwongAuthor Commented:
Thanks James. I will take your advice and leave it as is. Thanks again for your help. Much appreciated!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.