Click here to Skip to main content
15,886,648 members
Articles / Desktop Programming / Windows Forms

Display Any Usercontrol as a Popup Menu

Rate me:
Please Sign up or sign in to vote.
4.58/5 (51 votes)
1 May 20052 min read 232.3K   5.9K   131   47
This article introduces a little class which lets you show any usercontrol in XP style popup menu.

Introduction

I found it frustrating that the .NET Framework does not provide custom menus. I realized that you can owner-draw menus, but sometimes you just want to do more. I started with this little class which at the beginning was used for displaying a usercontrol in a floating window. Now, I have added several improvements to it.

  1. I wanted my popup to appear at the top of a button (like the Start menu in Windows). But only if there is enough place.
  2. The menu shows shadows (see the picture).
  3. The menu appears with a quick and graphically pleasing animation.

On the contrary of what I could find on the internet, this class lets you show any type of control. There is no need to derive from any special class. Also the popup will appear where you want it, on the screen. I used a button but you can use an image or anything that you like.

Using the code

To try the class, just run the Test project provided. I have tried to keep this class simple to use. To use it in your own project, you just need two lines of code. First, add the popup.vb file to your project.

If you want to display usercontrol1 when button1 is clicked, you just need these two lines of code:

VB
Private Sub Button1_Click(ByVal sender As System.Object, 
   ByVal e As System.EventArgs) 
      _ Handles Button1.Click
 popup = New Popup(New UserControl1, Button1)
 popup.Show()
End Sub

By default, the popup will appear animated with a shadow and a dark grey border.

You can change these settings if you want to. Note that the shadow part of this code was derived from a C# article called Add a drop shadow to a form by David M. Kean. This feature (and this feature only) will work only on Windows XP.

The popup will disappear automatically when its window is deactivated. The window is deactivated as soon as you click outside the popup. The component also offers two events DropDown and DropDownClosed. You can use this class to initialize or save your usercontrol values.

VB
Private Sub PopupDown(ByVal Sender As Object, ByVal e As EventArgs) _
          Handles popup.DropDown
 Me.Text = "Popup is open"
 End Sub

 Private Sub PopupClosed(ByVal Sender As Object, ByVal e As EventArgs) _
          Handles popup.DropDownClosed
 Me.Text = "Popup is closed"
 End Sub

Points of Interest

No rocket science, just a lightweight small class.

The positioning of the popup is more complicated than I expected, so you don't have to think about it.

History

  • 6 Feb. 2005 - First version released
    • The Resizable option does not resize very smoothly. The rest seems to work well.
  • 12 Feb. 2005
    • The popup does not appear in the task bar anymore (thank you steve).
    • The usercontrol can now prevent the popup from closing if it wants.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt, please contact the author via the discussion board below.

A list of licenses authors might use can be found here.


Written By
Software Developer (Senior)
France France
I am a French programmer.
These days I spend most of my time with the .NET framework, JavaScript and html.

Comments and Discussions

 
GeneralDrawGrip Pin
Laurent Muller10-May-05 20:05
professionalLaurent Muller10-May-05 20:05 
GeneralRe: DrawGrip Pin
Pascal Ganaye24-May-05 10:35
Pascal Ganaye24-May-05 10:35 
GeneralParent form looses focus Pin
Member 18600444-May-05 5:35
Member 18600444-May-05 5:35 
GeneralRe: Parent form looses focus Pin
Pascal Ganaye4-May-05 9:21
Pascal Ganaye4-May-05 9:21 
GeneralRe: Parent form looses focus Pin
Axel Rietschin26-Jun-05 1:33
professionalAxel Rietschin26-Jun-05 1:33 
GeneralRe: Parent form looses focus Pin
softwareguy7420-Feb-09 5:41
softwareguy7420-Feb-09 5:41 
GeneralClosing on click is a problem. Pin
Rüdiger Klaehn2-May-05 0:34
Rüdiger Klaehn2-May-05 0:34 
GeneralRe: Closing on click is a problem. Pin
Thomas Freudenberg2-May-05 1:30
Thomas Freudenberg2-May-05 1:30 
AFAIK you have to capture the mouse using Control.Capture[^]. (At least that's how I've done that back in my Win32 days Sigh | :sigh: )

Regards
Thomas


Disclaimer:
Because of heavy processing requirements, we are currently using some of your unused brain capacity for backup processing. Please ignore any hallucinations, voices or unusual dreams you may experience. Please avoid concentration-intensive tasks until further notice. Thank you.

GeneralRe: Closing on click is a problem. Pin
Dean_SF2-May-05 6:44
Dean_SF2-May-05 6:44 
GeneralRe: Closing on click is a problem. Pin
Pascal Ganaye2-May-05 9:16
Pascal Ganaye2-May-05 9:16 
GeneralRe: Closing on click is a problem. Pin
Rogier13-May-05 1:38
Rogier13-May-05 1:38 
GeneralRe: Closing on click is a problem. Pin
Pascal Ganaye13-May-05 7:44
Pascal Ganaye13-May-05 7:44 
GeneralRe: Closing on click is a problem. Pin
Anonymous14-May-05 2:18
Anonymous14-May-05 2:18 
GeneralRe: Closing on click is a problem. Pin
Pascal Ganaye14-May-05 6:05
Pascal Ganaye14-May-05 6:05 
GeneralClosing the PopMenu Pin
Bricoleu13-Apr-05 11:25
Bricoleu13-Apr-05 11:25 
GeneralJust what I was looking for... Pin
Simmy_NJ14-Feb-05 3:59
Simmy_NJ14-Feb-05 3:59 
GeneralRe: Just what I was looking for... Pin
Pascal Ganaye14-Feb-05 7:57
Pascal Ganaye14-Feb-05 7:57 
GeneralRe: Just what I was looking for... Pin
Simmy_NJ14-Feb-05 12:00
Simmy_NJ14-Feb-05 12:00 
GeneralA suggestion for Popup Pin
Stumpy84211-Feb-05 13:13
Stumpy84211-Feb-05 13:13 
GeneralRe: A suggestion for Popup Pin
Pascal Ganaye12-Feb-05 0:22
Pascal Ganaye12-Feb-05 0:22 

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.