Microsoft Communities

Welcome to WindowsClient.net | Sign in | Join

Here are some frequently asked questions about Windows Forms and their answers.

Windows Forms FAQs

How do I add a ContextMenu 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)
{ ... }

Contributed from George Shepherd's Windows Forms FAQ



Page view counter