Introduction

Popup windows are everywhere. Each tooltip is a popup window; each combobox has its popup list; many advertisements are also shown in popup windows.

How to Create a Popup Control in .NET?

At first, we might choose the Form class as a base class for our popup control. Unfortunately, it is a bad choice because when we show our popup form, the parent form loses its focus. A popup window should not cause that. Luckily, there is a class that does not cause loss of focus. We can use it as a base class for our popup control. It is the ToolStripDropDown class.

In this case, content is a control we want to show in a popup window. Of course, we have to remember to later dispose the popup window and its contents.

Popup Class

I wrote a Popup class that derives from ToolStripDropDown and simplifies the creating and managing of popup windows. The class calculates by itself where it should "pop" on the screen. It also disposes itself immediately after disposing the content control. To show a popup with a button, for example, we could write:

new Popup(new Button()).Show(someControl);

Here, someControl would be a control below which we want to show our popup.

Popup Resizing

To enable resizing for your popup, you must set the Resizable property to true and add the following code into your content control class:

You also have to set the ResizeRedraw property of the content control to true.

Important!

To specify the minimum and maximum size of the content control, please use the following properties:

content.MinimumSize and content.MaximumSize only inside the constructor of the content control

popup.MinimumSize and popup.MaximumSize elsewhere

ComboBox Class

The System.Windows.Forms.ComboBox class behaves in a strange way when it is on a popup control. It closes the popup control when the user clicks on a part of the combobox's dropdown that sticks out of a popup. So, I have created a PopupControl.ComboBox class that behaves properly.

PopupComboBox Class

This is a base class for comboboxes that can have a custom dropdown attached.

Animation Support

Animation is enabled by default. To change it, set the AnimationDuration, HidingAnimation, and ShowingAnimation properties.

Popup Members

Properties

AcceptAlt — Gets or sets a value indicating whether pressing the Alt key should close the popup.

AnimationDuration — Determines the duration of the animation.

Content — Gets the content of the popup.

FocusOnOpen — Gets or sets a value indicating whether the content should receive the focus after the popup has been opened.

HidingAnimation — Determines which animation to use while hiding the popup window.

MaximumSize — Gets or sets a maximum size of the popup.

MinimumSize — Gets or sets a minimum size of the popup.

NonInteractive — Gets or sets a value indicating whether the popup acts like a transparent windows; e.g., it cannot be clicked (note — it does not affect child controls).

Resizable — Gets or sets a value indicating whether the popup is resizable.

ShowingAnimation — Determines which animation to use while showing the popup window.

Constructor

Popup(Control content) — Initializes a new instance of the Popup class.

Comments and Discussions

You've stated that future work on the control will be done on codeplex; but I can't find it there.

Did you ever see history portrayed as an old man with a wise brow and pulseless heart, waging all things in the balance of reason?
Is not rather the genius of history like an eternal, imploring maiden, full of fire, with a burning heart and flaming soul, humanly warm and humanly beautiful?
--Zachris Topelius

Training a telescope on one’s own belly button will only reveal lint. You like that? You go right on staring at it. I prefer looking at galaxies.
-- Sarah Hoyt