illustrate
Pop-up menus are often used in programs, and multiple pop-up menus can exist in a form. Readers who have developed MDI forms may know that when the MDI subform is maximized, the menus of the subform and the main form can be automatically merged. How is this implemented? This example implements the function of dynamically combining two pop-up menus into one pop-up menu. The example effect is shown in Figure 1.2.
Important points
In C# 2.0, the pop-up menu has been encapsulated as a Context MenuStrip control, and the Items object in the control can be used to manipulate menu items in the menu. This object is of ToolStripMenuItem type. Use the ( ) method to add menu items to the pop-up menu. The prototype of this method is as follows.
public void AddRange ( ToolStripItem[] toolStripItems )
The parameters are described below.
toolStripItems: an array of controls.
process
(1) Create a project, name it Ex01_02, and the default form is Form1.
(2) Add a MenuStrip control to the Form1 form from the toolbox to design the menu; at the same time add a ContextMenuStrip control to the form to design the right-click menu; select the MenuStrip control to create a "Open child form" main menu, and then select the ContextMenuStrip control to add children to it.
(3) Add a form to the program, with the default name of Form2. At the same time, add a ContextMenuStrip control to the form to design the right-click menu, and then select the ContextMenuStrip control to add children to it.
private void Open from formToolStripMenuItem_Click(object sender, EventArgs e) { Form2 f = new Form2(); = this; ();//Display subform += new EventHandler(f_Resize); } void f_Resize(object sender, EventArgs e) { Form2 f = (Form2)sender; ToolStripMenuItem item = new ToolStripMenuItem(); for (int i = 0; i < f.; )//Merge menu { (f.[i]); } this.(new [] { item}); }
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.