Position transfer value of the ContextMenuStrip Control

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.