5,277,262 members and growing! (17,664 online)
Email Password   helpLost your password?
Desktop Development » Miscellaneous » Miscellaneous Controls     Intermediate License: The Common Public License Version 1.0 (CPL)

Simple Pop-up Control

By Lukasz Swiatkowski

How to create a custom pop-up control in C#
C# 2.0, C#, Windows, .NET, .NET 2.0VS2005, VS, Dev, Design

Posted: 3 Feb 2007
Updated: 5 May 2008
Views: 60,983
Announcements
Want a new Job?



Search    
Advanced Search
Sitemap
78 votes for this Article.
Popularity: 8.88 Rating: 4.69 out of 5
1 vote, 1.3%
1
3 votes, 3.8%
2
2 votes, 2.6%
3
7 votes, 9.0%
4
65 votes, 83.3%
5

Sample application using a custom pop-up control:

Custom pop-up control

Another application using a custom tooltip...

Custom tooltip

... and a more complex pop-up that can be resized:

Resizable pop-up

Tracking pop-up opening below the specified part of a control:

Tracking pop-up

Cascade of pop-up controls:

Cascade of pop-up controls

Introduction

Pop-up windows are everywhere. Each tooltip is a pop-up window; each combobox has its pop-up list; many advertisements are also shown in pop-up windows.

How To Create a Pop-up Control in .NET?

At first, we might choose the Form class as a base class for our pop-up control. Unfortunately, it's a bad choice because when we show our pop-up form, the parent form loses its focus. A pop-up window shouldn't cause that. Luckily, there is a class that does not cause loss of focus. We can use it as a base class for our pop-up control. It's 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 pop-up window. Of course, we have to remember to later dispose the pop-up window and its contents.

Popup Class

I wrote a Popup class that derives from ToolStripDropDown and simplifies the creating and managing of pop-up 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 pop-up 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 pop-up.

Pop-up Resizing

To enable resizing for your pop-up, 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 pop-up control. It closes the pop-up control when the user clicks on a part of the combobox's dropdown that sticks out of a pop-up. 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 pop-up
  • AnimationDuration: Determines the duration of the animation
  • Content: Gets the content of the pop-up
  • FocusOnOpen: Gets or sets a value indicating whether the content should receive the focus after the pop-up has been opened
  • HidingAnimation: Determines which animation to use while hiding the pop-up window
  • MaximumSize: Gets or sets a maximum size of the pop-up
  • MinimumSize: Gets or sets a minimum size of the pop-up
  • Resizable: Gets or sets a value indicating whether the pop-up is resizable
  • ShowingAnimation: Determines which animation to use while showing the pop-up 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 pop-up window below the specified control
  • void Show(Control control, Rectangle area): Shows the pop-up window below the specified area of the specified control

TO DO

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

History

  • 1.3 (04.05.2008)
    • Added AnimationDuration, HidingAnimation and ShowingAnimation properties
    • Removed UseFadeEffect property
    • Pop-up control can animate now even when the FocusOnOpen property is set to false
  • 1.2.5 (24.01.2008) – This is the last available version for Visual Studio 2005.
    • 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 pop-up
    • Added base class for a combobox that can have a custom dropdown, PopupComboBox
    • The sizing grip is automatically drawn if a pop-up is resizable
    • Added support for a minimum and maximum size of a resizable pop-up
    • 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, 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


Mvp
I'm studying computer science at 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.
Location: Poland Poland

Other popular Miscellaneous articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 25 of 113 (Total in Forum: 113) (Refresh)FirstPrevNext
Subject  Author Date 
GeneralThanksmemberAli Rafiee7:34 30 Jun '08  
Generalcapture Escape keydownmemberhamid_m21:31 29 Jun '08  
GeneralLove the control but the code is kinda hard to follow....memberchris17517:52 15 Jun '08  
Generalthank!memberMurderDolls8:16 2 Jun '08  
GeneralDateTime.Now edge casemembertoidy22:45 28 May '08  
GeneralSuppressUnmanagedCodeSecuritymembertoidy20:12 28 May '08  
GeneralTab KeymemberMeysam Naseri0:53 27 May '08  
GeneralRe: Tab KeymemberJimcheng4:34 30 May '08  
GeneralRe: Tab KeymemberI'm Chris1:43 8 Jun '08  
GeneralActivateOnShowmemberJonathan C Dickinson6:12 18 May '08  
GeneralSlide effectmemberJohnDoe1234567811:33 2 May '08  
GeneralRe: Slide effectmvpLukasz Swiatkowski6:28 3 May '08  
QuestionFocusmemberiPodSync9:55 23 Apr '08  
GeneralRe: FocusmvpLukasz Swiatkowski12:01 26 Apr '08  
GeneralResizememberDavidIPABD10:28 28 Jan '08  
GeneralRe: ResizemvpLukasz Swiatkowski12:12 22 Feb '08  
General"I'll post next version in January"memberDavidIPABD3:25 24 Jan '08  
GeneralRe: "I'll post next version in January"mvpLukasz Swiatkowski6:19 24 Jan '08  
GeneralClicking drop-button a with popup already showing hides then instantly reshows popup.memberNickB6:23 15 Jan '08  
GeneralRe: Clicking drop-button a with popup already showing hides then instantly reshows popup.memberPedro Henrique Liberato8:05 17 Jan '08  
GeneralRe: Clicking drop-button a with popup already showing hides then instantly reshows popup.mvpLukasz Swiatkowski8:59 17 Jan '08  
AnswerRe: Clicking drop-button a with popup already showing hides then instantly reshows popup. [modified]memberAli Rafiee10:02 1 Jul '08  
GeneralWhat does ToolStripDropDown bring compared to using UserControl or Control?memberwout de zeeuw4:53 28 Dec '07  
GeneralRe: What does ToolStripDropDown bring compared to using UserControl or Control?memberLukasz Swiatkowski3:16 2 Jan '08  
GeneralUsing a treeview in a Popup.ComboBoxmemberGravyBod6:13 11 Dec '07  

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

PermaLink | Privacy | Terms of Use
Last Updated: 5 May 2008
Editor: Deeksha Shenoy
Copyright 2007 by Lukasz Swiatkowski
Everything else Copyright © CodeProject, 1999-2008
Web18 | Advertise on the Code Project