Method of adding right click menu to C custom control

  • 2021-07-26 08:42:58
  • OfStack

C # Custom controls to add a right-click menu is very simple, mainly used controls, like control to define a right-click menu, items. add () superimposed right-click menu content, click event handler.

1. control is the control to define the right-click menu.

private void control_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
ContextMenu menu = new rightClickMenu (); //Initialize menu
menu. MenuItems. Add ("c1"); //Add menu item c1
menu. MenuItems. Add ("c2"); //Add menu item c2
menu. Show (control, new Point (e. X, e. Y); //Display menu at points (e. X, e. Y)
}
}
2. Add a right-click menu

class rightClickMenu : ContextMenuStrip
{
//Right-click menu
public rightClickMenu()
{
Items. Add ("Send a message"); //Add Menu Item 1
Items. Add ("send file"); //Add Menu Item 2
Items. Add ("disconnected"); //Add Menu Item 3

Items [0]. Click + = new EventHandler (sendMsg); //Define the Click event handler on menu item 1
Items [1]. Click + = new EventHandler (sendFile); //Define the Click event handler on menu item 2
Items [2]. Click + = new EventHandler (cutCon); //Define the Click event handler on menu item 3
}

//Send a message
private void sendMsg(object sender, EventArgs e)
{

}

//Send a file
private void sendFile(object sender, EventArgs e)
{

}

//Disconnect
private void cutCon(object sender, EventArgs e)
{

}
}

The above content is the method of adding right-click menu to C # custom control introduced in this paper. I hope everyone likes it.


Related articles: