Writing custom popup controls

Orangy asks:

Hello, thanks for nice articles. Could you please write one about creating combobox-style controls using WinForms? I mean control with a popup element, which is hidden when user clicks (or tabs, alt-tabs, win-button, hit global shortcut which activates something)somewhere outside it, like combo drop-down, popup menu and such. I've spent a lot of time trying to mimic this behavior including various WinAPI calls, hooks, modal loops, etc, but can't get it work just right.

Orangy - I feel your pain. If you're thinking about doing this in 1.1, it's going to be a major pain. The closest sample I've got for you is Raymond Chen's FakeMenu sample, which you would have to port over to managed code.  

But the news gets better in Windows Forms 2.0, you can just host *any* control within a ToolStripDropDown using ToolStripControlHost. I've talked a bit about a ComboBox which shows a TreeView using this method. You can ALSO handle the Closing event for the DropDown and check the ToolStripDropDown close reason, or permanently keep the DropDown open by setting AutoClose = false.

Here's the snippit which adds a TreeView to a ToolStripDropDown. The dropDown is later shown at the right time in the full sample via dropDown.Show(...).

ToolStripControlHost treeViewHost;
ToolStripDropDown dropDown;

public MyTreeViewCombo() {

TreeView treeView = new TreeView();
treeView.BorderStyle = BorderStyle.None;
treeViewHost = new ToolStripControlHost(treeView);

// create drop down and add it

dropDown = new ToolStripDropDown();
dropDown.Items.Add(treeViewHost);

}