Click here to Skip to main content
6,305,776 members and growing! (16,697 online)
Email Password   helpLost your password?
Desktop Development » Miscellaneous » Miscellaneous Controls     Intermediate License: The Common Public License Version 1.0 (CPL)

Simple Popup Control

By Lukasz Swiatkowski

How to create a custom pop-up control in C#
C# 2.0, Windows, .NET 2.0VS2005, Dev, Design
Version:5 (See All)
Posted:3 Feb 2007
Updated:16 Jan 2009
Views:106,190
Bookmarked:246 times
Announcements
Loading...
 
Search    
Advanced Search
printPrint   Broken Article?Report       add Share
  Discuss Discuss   Recommend Article Email
94 votes for this article.
Popularity: 9.29 Rating: 4.71 out of 5
1 vote, 1.1%
1
3 votes, 3.2%
2
3 votes, 3.2%
3
7 votes, 7.4%
4
80 votes, 85.1%
5

Sample application using a custom popup control:

Custom pop-up control

Another application using a custom tooltip...

Custom tooltip

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

Resizable pop-up

Tracking popup opening below the specified part of a control:

simplepopup.gif

Cascade of popup controls:

Cascade of pop-up 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:

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:

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:

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 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.
  • 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.4 (16.01.2009)
    • Added DroppedDown property, and DropDown, DropDownClosed events to the PopupComboBox class.
    • Fixed resizing of a popup when 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 AnimationDuration, HidingAnimation and ShowingAnimation properties.
    • Removed 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 popups.
    • 06.02.2007 – Added keyboard and custom region support, 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 Common Public License Version 1.0 (CPL)

About the Author

Lukasz Swiatkowski


Member
I am a graduate of Wroclaw University of Technology, Poland.

My interests: .NET, reading, programming, drawing, OBE and LD, Japan, yoga, tai-chi.
My favourite movie: "Star Trek: First Contact".

My website: www.lukesw.net
My email: lukasz.swiatkowski/*{at-sign}*/gmail.com.
Occupation: Software Developer
Location: Poland Poland

Other popular Miscellaneous articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 25 of 168 (Total in Forum: 168) (Refresh)FirstPrevNext
GeneralToolStripDropDown Not Showing Pinmemberwinheart22:47 28 Jun '09  
GeneralRe: ToolStripDropDown Not Showing PinmemberLukasz Swiatkowski8:24 29 Jun '09  
GeneralUse as a ToolTip PinmemberBob Carboni8:16 18 Jun '09  
GeneralRe: Use as a ToolTip PinmemberLukasz Swiatkowski10:50 24 Jun '09  
GeneralRe: Use as a ToolTip PinmemberBob Carboni14:47 24 Jun '09  
GeneralRe: Use as a ToolTip PinmemberLukasz Swiatkowski8:07 29 Jun '09  
GeneralMatters about using with MonthCalendar Pinmemberzmfeng(China)22:10 8 Jun '09  
Generalcommercial distribution question PinmemberSilent Winter4:15 5 May '09  
GeneralRe: commercial distribution question PinmemberLukasz Swiatkowski8:29 5 May '09  
GeneralRe: commercial distribution question PinmemberSilent Winter11:12 5 May '09  
GeneralRe: commercial distribution question PinmemberLukasz Swiatkowski11:49 5 May '09  
GeneralMono compatibility PinmemberKaSA16:55 31 Mar '09  
GeneralRe: Mono compatibility PinmemberLukasz Swiatkowski7:06 31 Mar '09  
GeneralSwietna robota Lukasz :) PinmemberPL011:01 12 Mar '09  
GeneralRe: Swietna robota Lukasz :) PinmemberLukasz Swiatkowski7:00 31 Mar '09  
GeneralNice article, though I disagree.... PinmemberGalatei14:23 13 Jan '09  
GeneralRe: Nice article, though I disagree.... Pinmemberdemogodyou23:00 14 Jan '09  
GeneralRe: Nice article, though I disagree.... PinmemberLukasz Swiatkowski14:41 15 Jan '09  
GeneralRe: Nice article, though I disagree.... PinmemberMichael Buen16:33 5 Mar '09  
QuestionUsing with a toolstripmenu PinmemberGee.2:07 13 Jan '09  
AnswerRe: Using with a toolstripmenu PinmemberGee.6:02 13 Jan '09  
AnswerRe: Using with a toolstripmenu PinmemberLukasz Swiatkowski14:54 15 Jan '09  
GeneralI have a problem.... Pinmembersven_minh18:59 20 Dec '08  
GeneralPopup will cause MDIChild lose focus Pinmemberchinkuanyeh22:48 24 Nov '08  
GeneralPopup closed when selecting an item from a combobox that drops below the bottom of the popup window PinmemberDaniel Gradwell6:08 19 Nov '08  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 16 Jan 2009
Editor: Sean Ewington
Copyright 2007 by Lukasz Swiatkowski
Everything else Copyright © CodeProject, 1999-2009
Web11 | Advertise on the Code Project