Click here to Skip to main content
13,199,669 members (63,384 online)
Click here to Skip to main content
Add your own
alternative version


55 bookmarked
Posted 9 Apr 2010

Creating a Form That Doesn't Take Focus

, 10 Apr 2010
Rate this:
Please Sign up or sign in to vote.
Popup forms that won't take focus even if clicked on, and cannot be alt+tabbed onto


I recently got interested in creating my own auto-complete drop-down/pop-up boxes. I refuse the method of using floating controls and rather enjoy the look and usefulness of small forms. Unfortunately I found that when used in conjunction with a textbox, there was trouble. When a form is shown, it's activated taking focus away from the textbox. This is NOT at all what I wanted. There are APIs that allow you to show a form without activating it. But that led to the problem of selecting data on the form which would then activate it. And I didn't like that behavior either. I was inspired by the Visual Studio Intellisense auto-complete box. It was a small, sizeable, bordered form with no title bar and no matter what you did, it never gained a focused appearance, nor did it take focus away from the IDE's text editor. THIS! THIS is what I wanted. Searching the net turned up some results, none of which worked correctly, some got close, and the ideas were there. I knew I needed to modify the style and extend style of the window. But what combination would yield the results. Google searching didn't help. Forums were a bust. So after reading the descriptions 100+ times, and a little trial and error, I found the combination.


Back in the world of Win32 programming with C++, you had to manually set styles of windows either when creating them, or by calling the SetWindowLong API in C#, with the .NET forms wrappers built in. We get to enjoy the properties like ShowInTaskbar that perform these calls behind the scenes. But as I have said before in previous articles, these simpler interfaces hide a little capability and so I live to circumvent restrictions. Fortune makes this simple, just override the CreateParams property of the Form. You can preserve your other settings by modifying the existing values, or if you know what you want, you can just manually set them all! But I recommend the latter.

Here is the standard pattern:

protected override CreateParams CreateParams
    CreateParams cp = base.CreateParams;
    cp.Style |= //Throw in some values!
    return cp;         

Using the Code

The trick to the Visual Studio style Intellisense form is the WindowsStyles WS_THICKFRAME and WS_CHILD in combination with the Extended Style WS_EX_NOACTIVATE and WS_EX_TOOLWINDOW. This combination gives you the ability to select data on the form using the mouse, but not worry about the form being activated.

protected override CreateParams CreateParams
     CreateParams ret = base.CreateParams;
     ret.Style = (int)Flags.WindowStyles.WS_THICKFRAME | 
     ret.ExStyle |= (int)Flags.WindowStyles.WS_EX_NOACTIVATE | 
     return ret;

Points of Interest

There are a few twists. The Form doesn't always size correctly on start up. Adding size and location to the createparams tends to take care of the problem, but isn't necessary as sizing the form once its show seems to work fine as well.

The source code includes a textbox control that shows an onscreen keyboard when the textbox has focus and features a numeric input mode as well, where a number pad appears and the textbox only accepts digits and a decimal.

It is important to note that WS_EX_NOACTIVATE alone will keep the form from gaining focus, so long as it's the ONLY form in the application. Once another form without that extended style is set is focused, the form will act normal until the other forms are deactivated.

So when creating an app with only 1 form, WS_EX_NOACTIVATE is good enough. And when used as a pop up, it simply isn't enough.

Some of the code in the demo is a modified version of code found on DaniWeb forum, VBAcceorator, and MSDN. Those code snippets belong to their respected authors and is noted in the comments.


  • v1 - Initial release


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


About the Author

Software Developer
United States United States
I am a part time programmer, The more I learn, the more I need to learn, the best of which I will share here on codeproject!

Visit my site and blog

You may also be interested in...


Comments and Discussions

PraiseWorks Great! Pin
partha chakraborti25-Jul-17 20:20
memberpartha chakraborti25-Jul-17 20:20 
QuestionSame thing under Delphi Pin
Member 413312716-Mar-17 1:07
memberMember 413312716-Mar-17 1:07 
Questionalso the popup form is shown in taskbar Pin
hambor30-Aug-11 17:42
memberhambor30-Aug-11 17:42 
QuestionFlags Pin
develophope8-Jul-11 9:20
memberdevelophope8-Jul-11 9:20 
GeneralThe popup form is shown in taskbar Pin
danbenedek16-Apr-10 1:25
memberdanbenedek16-Apr-10 1:25 
GeneralRe: The popup form is shown in taskbar Pin
Diamonddrake16-Apr-10 11:09
memberDiamonddrake16-Apr-10 11:09 
GeneralRe: The popup form is shown in taskbar Pin
danbenedek16-Apr-10 21:22
memberdanbenedek16-Apr-10 21:22 
GeneralRe: The popup form is shown in taskbar Pin
Diamonddrake17-Apr-10 16:12
memberDiamonddrake17-Apr-10 16:12 
GeneralRe: The popup form is shown in taskbar Pin
danbenedek17-Apr-10 20:53
memberdanbenedek17-Apr-10 20:53 
GeneralRe: The popup form is shown in taskbar Pin
Diamonddrake18-Apr-10 5:33
memberDiamonddrake18-Apr-10 5:33 
GeneralThanks Pin
pappukanghi11-Apr-10 22:04
memberpappukanghi11-Apr-10 22:04 
GeneralUsefull Pin
martyn_mcfly10-Apr-10 6:51
membermartyn_mcfly10-Apr-10 6:51 
GeneralAttachment link broken Pin
knoami9-Apr-10 22:41
memberknoami9-Apr-10 22:41 
GeneralRe: Attachment link broken Pin
reflex@codeproject10-Apr-10 3:38
memberreflex@codeproject10-Apr-10 3:38 
GeneralRe: Attachment link broken Pin
Diamonddrake10-Apr-10 6:17
memberDiamonddrake10-Apr-10 6:17 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.171020.1 | Last Updated 10 Apr 2010
Article Copyright 2010 by Diamonddrake
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid