Skip to main content
Email Password   helpLost your password?

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

You must Sign In to use this message board.
 
 
Per page   
 FirstPrevNext
GeneralDateTimePicker selection bug Pin
Jiong Mai
8:01 21 Jan '09  
GeneralThank you, Pascal! Pin
Big Dog
14:30 23 Apr '08  
QuestionModal Dialog Pin
E! Ray K
6:30 13 Mar '08  
QuestionShow from UserControl??? Pin
Lelo LXXIV
23:08 15 Nov '07  
QuestionC # version of this control Pin
Rei Masi
11:09 13 Nov '07  
AnswerRe: C # version of this control Pin
Big Dog
14:33 23 Apr '08  
GeneralA little Problem Pin
kapil bhavsar
4:06 4 Apr '07  
GeneralRe: A little Problem Pin
nzin
21:29 12 Aug '07  
NewsLicensing Pin
Pascal Ganaye
3:30 5 Mar '07  
GeneralRe: Licensing Pin
blumenhause
19:54 25 May '07  
GeneralTextbox search popup Pin
E! Ray K
11:03 23 Jan '07  
GeneralRe: Textbox search popup Pin
Pascal Ganaye
3:25 5 Mar '07  
GeneralRe: Textbox search popup Pin
connectpalm
2:50 3 Apr '07  
GeneralReally great, but... :) Pin
KStankov
19:49 14 Dec '06  
GeneralGreat control! Pin
blumenhause
8:12 28 Oct '06  
GeneralUsing Your Source Pin
Bernard C
1:15 20 Sep '06  
GeneralBlack Flickering Pin
codedieb
12:25 23 May '06  
GeneralRe: Black Flickering Pin
Pascal Ganaye
8:16 5 Jun '06  
GeneralVery nice control Pin
jasonpb
15:56 31 Jan '06  
GeneralClose on UserControl Button Click Pin
MartAlex
10:26 20 May '05  
GeneralRe: Close on UserControl Button Click [modified] Pin
codedieb
5:12 23 May '06  
GeneralAbout the Interface IPopupUserControl Pin
dyh2222
4:22 18 May '05  
GeneralDrawGrip Pin
Laurent Muller
21:05 10 May '05  
GeneralRe: DrawGrip Pin
pascal ganaye
11:35 24 May '05  
GeneralParent form looses focus Pin
2scoops.net
6:35 4 May '05  


Last Updated 1 May 2005 | Advertise | Privacy | Terms of Use | Copyright © CodeProject, 1999-2009