Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the realization method of dynamic merging of c # menu

2025-04-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article shows you what is the implementation of dynamic merging of the c # menu. The content is concise and easy to understand. It will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.

Description

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 MDI sub-forms are maximized, the menus of the sub-form and the main form can be merged automatically. How is this achieved? This example realizes the function of dynamically merging two pop-up menus into one pop-up menu. The effect of the example is shown in figure 1.2.

Keystone

The pop-up menu has been encapsulated as a Context MenuStrip control in C # 2.0, and the Items object in this control can be used to manipulate menu items in the menu. The object is of type ToolStripMenuItem, and you can add menu items to the pop-up menu using the Items.AddRange () method, which is based on the following prototype.

Public void AddRange (ToolStripItem [] toolStripItems)

The parameters are described below.

ToolStripItems: an array of controls.

Process

(1) create a project and name it Ex01_02, with the default form Form1.

(2) add a MenuStrip control to the Form1 form from the toolbox to design the menu; at the same time, add the ContextMenuStrip control to the form to design the right-click menu; select the MenuStrip control to create a "open sub-form" main menu, and then select the ContextMenuStrip control to add children to it.

(3) add a form to the program, the default name is Form2, and 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 opens from the form ToolStripMenuItem_Click (object sender, EventArgs e)

{Form2 f = new Form2 (); f.MdiParent = this; f.Show (); / / Show sub-form

F.Resize + = 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.contextMenuStrip2.Items.Count;) / / merge menu

{item.DropDownItems.Add (f. ContextMenuStrip2.Items [I])

}

This.contextMenuStrip1.Items.AddRange (new System.Windows.Forms.ToolStripItem [] {

Item})

}

The above content is what is the implementation of dynamic merging of c # menus? have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report