Click here to Skip to main content
15,898,222 members
Articles / Programming Languages / C#

Simple Popup Control

Rate me:
Please Sign up or sign in to vote.
4.93/5 (167 votes)
26 Mar 2013LGPL35 min read 1M   24.6K   523   243
How to create a custom pop-up control in C#.
Sample application using a custom popup control:

Custom popup control

Another application using a custom tooltip...

Custom tooltip

... and a more complex popup that can be resized:

Resizable popup

Tracking popup opening below the specified part of a control:

Tracking popup

Cascade of popup controls:

Cascade of popup controls

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.

How to Use the ToolStripDropDown Class?

This would be the simplest way, without deriving from that class:

C#
ToolStripDropDown popup = new ToolStripDropDown();
popup.Margin = Padding.Empty;
popup.Padding = Padding.Empty;
ToolStripControlHost host = new ToolStripControlHost(content);
host.Margin = Padding.Empty;
host.Padding = Padding.Empty;
popup.Items.Add(host);
popup.Show(parentForm, location);

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:

C#
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:

C#
protected override void WndProc(ref Message m)
{
    if ((Parent as Popup).ProcessResizing(ref m)) return;
    base.WndProc(ref m);
}

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.

Methods

  • void PaintSizeGrip(PaintEventArgs e) — Paints the sizing grip.
  • bool ProcessResizing(ref Message m) — Processes the resizing messages.
  • void Show(Control control) — Shows the popup window below the specified control.
  • void Show(Control control, Rectangle area) — Shows the popup window below the specified area of the specified control.

TODO

  • Base class for custom tooltips.
  • Office 2007-like tooltip class.

History

  • 1.5 (20.10.2010) — Important! This is the last “standalone” version of the control. The next version is included in a new project hosted at CodePlex.
    • Added the NonInteractive property to the Popup class.
    • Fixed resizing on DualView/multi monitor systems.
    • Popup is now always shown on top of other windows (previously the popup could be shown below its parent during the animation process).
    • PopupComboBox didn’t properly set focus to its dropdown on Windows 7.
    • Improved compatibility with Mono.
    • Uses C# 3.0 syntax (auto-properties, lambdas).
    • Signed binaries for both .NET 2.0 and .NET 4.0 are available.
    • Solution upgraded to the Visual C# 2010 format.
    • License changed to LGPL 3.0.
  • 1.4 (16.01.2009)
    • Added the DroppedDown property, and the DropDown, DropDownClosed events to the PopupComboBox class.
    • Fixed resizing of a popup when the MaximumSize property of the content control is not set.
  • 1.3.1 (20.09.2008)
    • Tab-key properly transfers the focus between controls contained in a popup window.
    • Minor bugs fixed.
  • 1.3 (04.05.2008)
    • Added the AnimationDuration, HidingAnimation, and ShowingAnimation properties.
    • Removed the UseFadeEffect property.
    • Popup control can animate now even when the FocusOnOpen property is set to false.
  • 1.2.5 (24.01.2008)
    • Fixed “Alt+F4” bug.
    • Fixed drawing the sizing grip.
    • Minor bugs fixed.
  • 1.2 (24.07.2007)
    • Added animation support.
    • Added AcceptAlt property.
    • Clicking on the non-client area bug fixed.
    • Minor bugs fixed.
  • 1.1 (05.07.2007)
    • Added XML documentation.
    • "Fixed" the ComboBox class, so it can be used inside a popup.
    • Added base class for a combobox that can have a custom dropdown, PopupComboBox.
    • The sizing grip is automatically drawn if a popup is resizable.
    • Added support for a minimum and maximum size of a resizable popup.
    • Minor bugs fixed.
  • 1.0
    • 08.06.2007 – Added resizing support and capability for using cascading pop-ups.
    • 06.02.2007 – Added keyboard and custom region support, and the ShowForControl method name changed to Show.
    • 03.02.2007 – First version.

License

This article, along with any associated source code and files, is licensed under The GNU Lesser General Public License (LGPLv3)


Written By
Software Developer
Poland Poland
I am a graduate of Wroclaw University of Science and Technology, Poland.

My interests: gardening, reading, programming, drawing, Japan, Spain.

Comments and Discussions

 
GeneralRe: commercial distribution question Pin
Lukasz Sw.5-May-09 10:49
Lukasz Sw.5-May-09 10:49 
GeneralMono compatibility Pin
KaSA131-Mar-09 5:55
KaSA131-Mar-09 5:55 
GeneralRe: Mono compatibility Pin
Lukasz Sw.31-Mar-09 6:06
Lukasz Sw.31-Mar-09 6:06 
GeneralSwietna robota Lukasz :) Pin
pluniewski12-Mar-09 0:01
pluniewski12-Mar-09 0:01 
GeneralRe: Swietna robota Lukasz :) Pin
Lukasz Sw.31-Mar-09 6:00
Lukasz Sw.31-Mar-09 6:00 
GeneralNice article, though I disagree.... Pin
Galatei13-Jan-09 13:23
Galatei13-Jan-09 13:23 
GeneralRe: Nice article, though I disagree.... Pin
RayShaw14-Jan-09 22:00
RayShaw14-Jan-09 22:00 
GeneralRe: Nice article, though I disagree.... Pin
Lukasz Sw.15-Jan-09 13:41
Lukasz Sw.15-Jan-09 13:41 
Thank you for your constructive message.

Galatei wrote:
2. Popup windows are different from POP-UPs. POP-UPs are exploited by advertising companies to steal user's focus (attention) and display whatever they were created for. Popup windows are tools that should be used for specific purposes, mostly informative, like tooltips or contextual help.


I agree. I submitted an updated article in which I changed all occurrences of pop-up to popup since the article is not about ads.

Galatei wrote:
1. I disagree that the popup window shouldn't steal focus from its parent. Who decided that popup windows shouldn't steal focus? Which windows is stealing focus and which isnt't depends on the situation and specific needs.
In fact, logically thinking, there should be only one control on the screen with focus - this is one of the principles of Windows programming. If you want do allow user to do something in your popup, like select something from combobox (one of the examples), then focus should migrate to that combobox and return it back when the popup is closed.

3. Creating applications that leave users with few controls having focus at the same time on the screen is wrong and misleading.


Remember that the popup window should be a small part of a control/window, it should be light, so it could be treated like a real part of the control it belongs to (just like popup list of the combobox, popupmenu of the window), and as a part of that control it shouldn't steal "visual focus" (I explained it further). I admit that my example "more complex popup" is a little too complex for a popup window and it should either steal focus or be transformed into a dialog box. I also should've written that "not stealing focus" means only that the title bar of the window is not redrawn as an inactive (the "visual focus" is not stolen, but the actual focus is). The focus is only in one window at once, so if the popup is not a tooltip, but some interactive popup (like combobox list, menu, popup menu), and user clicks on that popup, the popup steals focus from the main window, but prevents the title bar from redrawing as inactive (or does not steal focus at all, just like popup menus don't).
Only the separate parts of the application (like other windows or dialog boxes) should steal focus from the main window. Well, that my personal opinion on that subject.

Galatei wrote:
Imagine disabled people using someone's software designed with such idea in mind, who's having troubles using it, because there's 5 popups on top of the window with focus - which have focus too (that's just one example).

I'd rather urge developers to learn proper UI design techniques, and treat such ideas with some distance, always trying to develop application with all people in mind (which doesn't mean they have to be ugly or not have fancy controls). You can't even imagine how many applications on the market are not suitable for disabled people, or people with little computer use knowledge - believe me - more than 75%.


Hm... creating UI for disabled people can be tricky. It depends on the disablement (I hope it's the correct word). Personally, I always try to implement controls in way that they can be used either by mouse or by keyboard, but I don't if it's enough for disabled people.
I know that many applications are not suitable for people either disabled or with little knowledge. Many applications have terribly unclear or messed UI, because unfortunately some programmers think only about functionality, not usability.
It's great that Microsoft created UX Guide in which they explains how to create clear and usable UI. It should be read by every programmer which creates UI for Windows applications.

Once more, thanks for your post Smile | :)

Regards,
Łukasz

~~~~~~~~~~~~~~~~~~~
My website: www.lukesw.net

GeneralRe: Nice article, though I disagree.... Pin
Michael Buen5-Mar-09 15:33
Michael Buen5-Mar-09 15:33 
QuestionUsing with a toolstripmenu Pin
Gee.13-Jan-09 1:07
Gee.13-Jan-09 1:07 
AnswerRe: Using with a toolstripmenu Pin
Gee.13-Jan-09 5:02
Gee.13-Jan-09 5:02 
AnswerRe: Using with a toolstripmenu Pin
Lukasz Sw.15-Jan-09 13:54
Lukasz Sw.15-Jan-09 13:54 
GeneralI have a problem.... Pin
sven_minh20-Dec-08 17:59
sven_minh20-Dec-08 17:59 
GeneralPopup will cause MDIChild lose focus Pin
chinkuanyeh24-Nov-08 21:48
chinkuanyeh24-Nov-08 21:48 
GeneralPopup closed when selecting an item from a combobox that drops below the bottom of the popup window Pin
Daniel Gradwell19-Nov-08 5:08
Daniel Gradwell19-Nov-08 5:08 
GeneralRe: Popup closed when selecting an item from a combobox that drops below the bottom of the popup window Pin
Lukasz Sw.19-Nov-08 7:30
Lukasz Sw.19-Nov-08 7:30 
QuestionInsersion Gripper into the toolstrip Pin
rixwan19-Nov-08 3:46
rixwan19-Nov-08 3:46 
Generalcontent Leave event won't fire Pin
Brian Perrin14-Nov-08 2:06
Brian Perrin14-Nov-08 2:06 
GeneralRe: content Leave event won't fire Pin
Lukasz Sw.19-Nov-08 8:02
Lukasz Sw.19-Nov-08 8:02 
QuestionOpen Dialog window from within popup Pin
Daniel Gradwell10-Nov-08 5:04
Daniel Gradwell10-Nov-08 5:04 
AnswerRe: Open Dialog window from within popup Pin
Lukasz Sw.19-Nov-08 7:55
Lukasz Sw.19-Nov-08 7:55 
Generalgoog Pin
pophelix24-Sep-08 5:36
pophelix24-Sep-08 5:36 
GeneralRe: goog Pin
Lukasz Sw.24-Sep-08 7:39
Lukasz Sw.24-Sep-08 7:39 
Generaltooltip "flicker" (focus duel) Pin
Brian Perrin6-Sep-08 12:03
Brian Perrin6-Sep-08 12:03 
GeneralRe: tooltip "flicker" (focus duel) Pin
Lukasz Sw.9-Sep-08 15:08
Lukasz Sw.9-Sep-08 15:08 

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.