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, its
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 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, non of which worked correctly, some got close, and the ideas where there. I knew i needed to modify the style and extend style of the window. but what combination would yield the resuts. 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 preserver 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 |= //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
{
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 a 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.
its Important to note that
WS_EX_NOACTIVATE alone will keep the form from gaining focus, so long as its 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 are 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.
,
This attachment is hidden for guests. Please log in or register to see it.
This attachment is hidden for guests. Please log in or register to see it.