The common method for this control is to directly set the ContextMenuStrip attribute of its subordinate control to contextMenuStrip1 to ensure the correctness of the right-click menu position.
However, sometimes we need to set the position of ContextMenuStrip to display the right-click menu at a specific location of a container;
S1. declare a right-click menu control;
S2. add the right-click menu content;
S3. set the right-click menu to follow the mouse clicking position. The key is the Coordinate Transformation of the location; oView. pointToScreen (e. location); The oView object must be correctly selected, that is, the right-click container should appear, and its coordinate conversion method should be used to get the right-click Location;
This ensures that the right-click menu appears correctly.
The following code is provided:
ContextMenuStrip cms = new ContextMenuStrip (); cms. items. add ("attribute settings"); // coordinate conversion ensures that the right-click menu appears at the clicked position Point p = oView. pointToScreen (e. location); cms. show (p); cms. items [0]. tag = oView; cms. items [0]. click + = ContextMenuStrip_Click;
In addition, I encountered another problem during the Development. I right-Click to set the attribute of an object. However, what should I do if this object cannot be obtained in this Click event? I tried to use the Tag attribute to pass the required value to the click event for processing. The result is OK.
// Right-click the event private void ContextMenuStrip_Click (object sender, EventArgs e) {XX view = (ToolStripDropDownItem) (sender). Tag as XX ;}
It is not completely thorough and is for reference only. If you have a better method, please leave a message. Thank you.