Serialcoder en Français Serialcoder in English
TEL : +33 (0)9 72 13 15 17

Windows Forms FAQ resources

10. Windows Forms Menus

10.2 How do I add a context menu to a control?


The frame will manage a context menu for you if you set the control's ContextMenu property. Here is some code adding a context menu to a Label.

     //Add Menus in your form's constructor...
     this.pictureContextMenu = new ContextMenu();
     this.pictureContextMenu.MenuItems.Add("&Color",
               new EventHandler(Color_Clicked));
     this.pictureContextMenu.MenuItems.Add("&Font",
               new EventHandler(Font_Clicked));
     label1.ContextMenu = this.pictureContextMenu;
     //
     // TODO: Add any constructor code after InitializeComponent call
     //
}
     
private void Font_Clicked(object sender, System.EventArgs e)
{ . . . }

private void Color_Clicked(object sender, System.EventArgs e)
{ ... }