Thisevent can be handled to customize the ContextMenu before it is shown tothe user. When a ContextMenu is invoked, a Popup event is fired.
The following code demonstrates how to add a ContextMenu at runtime: C#ContextMenu cMenu = new ContextMenu() MenuItem menuItem1 = new MenuItem() MenuItem menuItem2 = new MenuItem() menuItem1.Text = "Default Item 1" menuItem2.Text = "Default Item 2" // Add menuItem2 as a child of (nuItem2) // Add menuItem1 to the context (nuItem1) // Add the context menu to a label controllabel1.ContextMenu = cMenu VBDim cMenu = new ContextMenu()Dim menuItem1 = new MenuItem()Dim menuItem2 = new MenuItem()menuItem1.Text = "Default Item 1"menuItem2.Text = "Default Item 2"' Add menuItem2 as a child of (menuItem2)' Add menuItem1 to the context (menuItem1)' Add the context menu to a label controllabel1.ContextMenu = cMenu Next add MenuItemobjects to it, and then set the ContextMenu property of a control tothe ContextMenu instance. First, a ContextMenu control must be instantiated. You can also create ContextMenu controls at runtime. The Edit Menu link is visible only when the ContextMenu isselected. To add MenuItem to the ContextMenu, click the ContextMenu on the Form or click the Edit Menu link in the Propertieswindow. At runtime the ContextMenu will appear above its respectivecontrol. When the ContextMenu is selected, the Form Designer paints the control in thesame place as the MainMenu control. The new ContextMenu control willappear in the panel at the bottom of the Form Designer. To add a ContextMenu to an application, drag the control from the ToolBox onto the application.
ContextMenus are also known as pop-up menus that appear when youtap-and-hold a control on Pocket PC or right-click a control on Windows CE.Tap-and-hold can be simulated on the Pocket PC emulator by pressing and holdingthe left mouse button. The ContextMenu control is almost exactly like the MainMenucontrol except that the context menus are associated with other controls,whereas the MainMenu is associated with the application's form. Learn More Buy Using a ContextMenu Control in an Application Using the HScrollBar and VScrollBar Controls.Using the OpenFileDialog and SaveFileDialog Controls.Using a ContextMenu Control in an Application Understanding the Different Windows Forms Target Platforms.Investigating Unsupported Functionality in the.Investigating Unsupported Controls in the.
If (hitTestInfo.Type = DataGridViewHitTestType.Cell & hitTestInfo.ColumnIndex = 1)ĬontextMenuForColumn2.Show(dataGridView, new Point(e.X, e. If (hitTestInfo.Type = DataGridViewHitTestType.Cell & hitTestInfo.ColumnIndex = 0)ĬontextMenuForColumn1.Show(dataGridView, new Point(e.X, e.Y))
HitTestInfo = dataGridView.HitTest(e.X, e.Y) Load context menu on right mouse click Private void dataGridView_MouseUp( object sender, MouseEventArgs e) Private void Form_Load( object sender, EventArgs e)Ĭ( "Make Active", new EventHandler(MakeActive)) Ĭ( "Delete", new EventHandler(Delete)) Ĭ( "Register", new EventHandler(Register)) Īdd the following code to mouseup event of the gridview: Private ContextMenu contextMenuForColumn2 = new ContextMenu() Īdd the following line of code in the form load event: Private ContextMenu contextMenuForColumn1 = new ContextMenu() Define different context menus for different columns The menu items may vary from column to column of the gridview. One of the projects had a requirement to create a context menu on mouse click over a DataGridView having employee details.