Introduction
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.
Background
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
{
get
{
CreateParams cp = base.CreateParams;
cp.Style |= 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
{
get
{
CreateParams ret = base.CreateParams;
ret.Style = (int)Flags.WindowStyles.WS_THICKFRAME |
(int)Flags.WindowStyles.WS_CHILD;
ret.ExStyle |= (int)Flags.WindowStyles.WS_EX_NOACTIVATE |
(int)Flags.WindowStyles.WS_EX_TOOLWINDOW;
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.
History