Left button can also pop up menu

zhaozj2021-02-16  56

Add a pop-up menu in C # .NET is very simple, by dragging a contextMenu into the form from the Windows Form control, then specify the ContextMenu property for the corresponding control, pop-up menus when you right-click the control. , Set the method as shown in the figure:

figure 1

How can I pop up the menu?

Use the contextMenu.show method to display the menu; the Point structure defines the location of the menu and related controls.

The steps are as follows (I use SharpDevelop, VS.NET is similar):

1. Creating a new composite project, select Windows Form Project,

figure 2

2. Drag a ContextMenu control on the form and a Button control.

ContextMenu's Name is changed to MyContextMenu and add several menu items.

Change the button of Button to MyButton, set to (100, 80) I will take the Button control as an example, and the other control methods are similar.

image 3

3. Add events for the button

Void MyButtonClick (Object Sender, System.EventArgs E)

{

Point P = New Point (0, 0);

This.myContextMenu.Show (MyButton, P);

}

Compile, run the results. When you click the button, you will pop up the menu.

Figure 4

4. Notice the Point structure in the above code, the POINT structure represents an orderly pair of integer X and Y coordinates in the two-dimensional plane. The above is (0, 0), not to prevent it (50, 40) to see if it is displayed in the middle.

The Show method defines the public void show (Control Control, Point POS); parameter: A Control object, which specifies the control associated with this shortcut menu. A Point object, which specifies the coordinates of the location where the menu is displayed. These coordinates are specified relative to the workspace coordinates of the controls specified in the Control parameter.

Figure 5

5. At this time, our pop-up menu is completed. :)

转载请注明原文地址:https://www.9cbs.com/read-22686.html

New Post(0)