Click here to Skip to main content
Licence 
First Posted 6 Feb 2005
Views 153,282
Bookmarked 110 times

Display any usercontrol as a popup menu

By | 1 May 2005 | Article
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:

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.

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

About the Author

Pascal Ganaye

Web Developer

United Kingdom United Kingdom

Member

I am a French programmer.
These days I spend most of my time within the .NET framework.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralUsing Your Source PinmemberBernard C0:15 20 Sep '06  
GeneralBlack Flickering Pinmembercodedieb11:25 23 May '06  
GeneralRe: Black Flickering PinmemberPascal Ganaye7:16 5 Jun '06  
GeneralVery nice control Pinmemberjasonpb14:56 31 Jan '06  
GeneralClose on UserControl Button Click PinmemberMartAlex9:26 20 May '05  
GeneralRe: Close on UserControl Button Click [modified] Pinmembercodedieb4:12 23 May '06  
GeneralAbout the Interface IPopupUserControl Pinmemberdyh22223:22 18 May '05  
I am a beginner.I couldn't understand the interface IPopupUserControl.In the sub:
Protected Overrides Sub OnDeactivate(ByVal e As System.EventArgs)
MyBase.OnDeactivate(e)
If mClosing = False Then
If TypeOf Me.mPopup.mUserControl Is IPopupUserControl Then
mClosing = DirectCast(Me.mPopup.mUserControl, IPopupUserControl).AcceptPopupClosing()
Else
mClosing = True
End If
If mClosing Then DoClose()
End If
End Sub
I have questions as follows:
1、I don't know In any case the type of mUserControl would equal
IPopupUserControl
2、I think that AcceptPopupClosing is never been set value,because there is no value was endowed with it.
I would really appriciate if you tell me.
GeneralDrawGrip PinmemberLaurent Muller20:05 10 May '05  
GeneralRe: DrawGrip Pinmemberpascal ganaye10:35 24 May '05  
GeneralParent form looses focus Pinmember2scoops.net5:35 4 May '05  
GeneralRe: Parent form looses focus Pinmemberpascal ganaye9:21 4 May '05  
GeneralRe: Parent form looses focus Pinmemberaxelriet1:33 26 Jun '05  
GeneralRe: Parent form looses focus Pinmemberdbassett745:41 20 Feb '09  
GeneralClosing on click is a problem. PinmemberRüdiger Klaehn0:34 2 May '05  
GeneralRe: Closing on click is a problem. PinmemberThomas Freudenberg1:30 2 May '05  
GeneralRe: Closing on click is a problem. PinmemberDean_DOT6:44 2 May '05  
GeneralRe: Closing on click is a problem. Pinmemberpascal ganaye9:16 2 May '05  
GeneralRe: Closing on click is a problem. PinmemberRogier1:38 13 May '05  
GeneralRe: Closing on click is a problem. Pinmemberpascal ganaye7:44 13 May '05  
GeneralRe: Closing on click is a problem. PinsussAnonymous2:18 14 May '05  
GeneralRe: Closing on click is a problem. Pinmemberpascal ganaye6:05 14 May '05  
GeneralClosing the PopMenu PinmemberBricoleu11:25 13 Apr '05  
GeneralJust what I was looking for... PinmemberSimmy_NJ3:59 14 Feb '05  
GeneralRe: Just what I was looking for... Pinmemberpascal ganaye7:57 14 Feb '05  
GeneralRe: Just what I was looking for... PinmemberSimmy_NJ12:00 14 Feb '05  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120528.1 | Last Updated 2 May 2005
Article Copyright 2005 by Pascal Ganaye
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid