C # implement the "left-click" pop-up menu

Source: Internet
Author: User

 

When developing applications, we often need to use the pop-up menu. Sometimes, you need to implement this function when you click a control.

. NET provides a ContextMenuStrip control that can be bound to other controls. When you click this control, the corresponding menu is displayed.

However, there is a problem in doing so. The trigger condition of ContextMenuStrip is that the control to which it is bound is right-clicked.

If all interface operations of an application are completed by clicking or double-clicking, you must right-click a widget. This situation is a bit unpleasant.

Is there any way to trigger the pop-up menu by clicking the left button? Of course, the answer is yes. This method is described as follows:

  1. First, create a ContextMenuStrip, get the name, and set the relevant attributes. Here we name the control ctxMenu
  2. Next, create a control to trigger the menu, which can be any control. Here we use a Button control named btnMenu.
  3. Then there is the code part. A very simple code:
Private void btnMenu_Click (object sender, EventArgs e)
{
// Parameter 1 is the bound control
// Parameter 2 indicates the position where the pop-up menu is displayed (0, 0 matches the origin of the bound control)
CtxMenu. Show (btnMenu, new Point (0, btnMenu. Height + 5 ));
}

Note: of course, this code is not perfect, because the menu is displayed either by clicking the left button or by right-clicking. You can write the code to the mouse event of the control, click the left and right buttons to solve the problem.

Related Article

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.