473,399 Members | 2,159 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

Preventing a form from getting focus

Is there a way to prevent a form from getting focus?

Thanks!
- Pete
Nov 16 '05 #1
5 10093
"Petec" <x@x.x> wrote:
Is there a way to prevent a form from getting focus?


Why do you want to do this? There is probably a better solution than
preventing the user from doing what he wants to do.

P.
Nov 16 '05 #2
Paul E Collins wrote:
"Petec" <x@x.x> wrote:
Is there a way to prevent a form from getting focus?


Why do you want to do this? There is probably a better solution than
preventing the user from doing what he wants to do.

P.


I have a main form with a grid of items, and when the user hovers over an
item a ballon with a detailed description of the item pops up.
I'd like to prevent the balloon from getting focus, so that the main form
still has focus.

- Pete
Nov 16 '05 #3
"Petec" <x@x.x> wrote:
I have a main form with a grid of items, and when
the user hovers over an item a ballon with a detailed
description of the item pops up.
I'd like to prevent the balloon from getting focus, so
that the main form still has focus.


Hovering involves use of the mouse, so if the user then clicks on
something else, that click will give focus back to the main form
anyway.

If that's not good enough, I suppose you could make the balloon
'always on top' (TopMost property) and automatically restore focus to
the main form as soon as the balloon loads.

P.
Nov 16 '05 #4
I could be wrong, but I don't think there's any managed code to do this
unfortunately (I've been looking for a managed solution to this problem for
a long time.)

What you could do is make an API call to ShowWindow.

You first have to bring in ShowWindow in your source with something that
looks like this:

[DllImport("user32.dll")]
private static extern Boolean ShowWindow(IntPtr hWnd,Int32 nCmdShow);

Now you can use it. Instead of showing your form the conventional way, show
it like this:

ShowWindow(this.Handle, 4);

Now it will be shown without stealing focus.

I got this code from John O'Byrne from an excellent article on
CodeProject.com for an MSN-style skinnable popup window. You can find the
full source here:

http://www.codeproject.com/cs/miscct...arnotifier.asp

The only change that I'd recommend to stay consistent with Win32 programming
and keeping your code more elegant is to declare SW_SHOWNOACTIVATE as a
constant somewhere in your code instead of hardcoding "4" for the show
state.

ShowWindow(this.Handle, SW_SHOWNOACTIVATE);

Looks nicer. Otherwise that guy did a great job on his component and you
should check it out whether you need something like that or not.

Calling ShowWindow is a much better solution than setting focus back to the
main form, by the way. I've tried that. You get some problems with the
user's input being momentarily intercepted and flashing in some cases and
some obvious quirks. But the above works beautifully. I REALLY hope they
will provide a better solution for this in .NET 2.0, or perhaps wrap
ShowWindow() in WinFX.

"Petec" <x@x.x> wrote in message
news:d7****************@newsread1.news.pas.earthli nk.net...
Is there a way to prevent a form from getting focus?

Thanks!
- Pete

Nov 16 '05 #5
Thank you, that worked great!

- Pete

Eric wrote:
I could be wrong, but I don't think there's any managed code to do
this unfortunately (I've been looking for a managed solution to this
problem for a long time.)

What you could do is make an API call to ShowWindow.

You first have to bring in ShowWindow in your source with something
that looks like this:

[DllImport("user32.dll")]
private static extern Boolean ShowWindow(IntPtr hWnd,Int32 nCmdShow);

Now you can use it. Instead of showing your form the conventional
way, show it like this:

ShowWindow(this.Handle, 4);

Now it will be shown without stealing focus.

I got this code from John O'Byrne from an excellent article on
CodeProject.com for an MSN-style skinnable popup window. You can find
the full source here:

http://www.codeproject.com/cs/miscct...arnotifier.asp

The only change that I'd recommend to stay consistent with Win32
programming and keeping your code more elegant is to declare
SW_SHOWNOACTIVATE as a constant somewhere in your code instead of
hardcoding "4" for the show state.

ShowWindow(this.Handle, SW_SHOWNOACTIVATE);

Looks nicer. Otherwise that guy did a great job on his component and
you should check it out whether you need something like that or not.

Calling ShowWindow is a much better solution than setting focus back
to the main form, by the way. I've tried that. You get some problems
with the user's input being momentarily intercepted and flashing in
some cases and some obvious quirks. But the above works beautifully.
I REALLY hope they will provide a better solution for this in .NET
2.0, or perhaps wrap ShowWindow() in WinFX.

"Petec" <x@x.x> wrote in message
news:d7****************@newsread1.news.pas.earthli nk.net...
Is there a way to prevent a form from getting focus?

Thanks!
- Pete


Nov 16 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: alanstew | last post by:
With the body tag calling out 'window onload', a function with a 'window.open' fails at the 'window.open' line. If I cut out the body tag, the function executes as normal. At first I thought it...
5
by: Darren DeCoste | last post by:
I have an access project with a Form that has a Command button to print the current record. I would like to Print the record 3 times, changing a text field on each print. The standard code of...
8
by: Susan Bricker | last post by:
Hi. I am opening a form (frmUserSettings) just before (or so I thought) the Main Menu form (frmMainMenu) is opening but it's not getting the focus. It gets "painted" and then the Main Menu gets...
13
by: M O J O | last post by:
Hi, I need to create a popup form what will not steal focus. I've searched the net and I managed to put some code together. Below is the result of my code, but I have a single question .......
0
by: Dave | last post by:
Hello, I am writing an On-screen keyboard, similar to the one included in windows 2k and XP. The problem I am having is that I need my keyboard to never get the focus, but still be able to...
2
by: Khadim | last post by:
Hi, I am having this problem with my application that it is not getting itself in taskbar by itself.. i m working on this application for 5 months, don't know what happened at what time that its...
2
by: Sid Price | last post by:
Is there a way of stopping a form getting focus in VB.NET. The scenario I have is a main form and a form used for display only. There are no user controls on the display form and it does not ever...
3
by: sravan_reddy001 | last post by:
i want to prevent a form from closing.. to do this i want to handle the formClosing or FormClosed events. from here i want to prevent the form from closing. New instance of same form should...
2
by: Joergen Bech | last post by:
Hope someone has a solution or some suggestions for this. This cannot be right?!? Problem: I have multiple non-modal forms open at the same time. One or more of these forms have a...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.