1. Form and interface design-menu application example

Source: Internet
Author: User

Menus are frequently used interface elements in program development. Using menus properly not only makes it easy for users to use program functions, but also improves work efficiency.

001 menu with historical information

To save the most recently opened file, you can save the file name and path of the most recently opened file in the menu to the previously created *. in the ini file, the software is read at startup *. you can create an array menu for the data in the ini file to display the history menu.

: To create a menu with historical information, you must first add a MenuStrip Menu Control, create a "file" main menu, and create menu options such as opening, closing, and exiting under it. Add the OpenFileDialog control to the form and set the IsMdiContainer attribute of the main form to True.

Namespace _ 001_MenuHistory {public partial class Form1: Form {string address; public Form1 () {InitializeComponent (); address = System. environment. currentDirectory;} // write the open file path to the INI file private void open ToolStripMenuItem_Click (object sender, EventArgs e) {openFileDialog1.FileName = ""; // set the initialization content of the open file dialog box to null this. openFileDialog1.ShowDialog (); // display the StreamWriter s = new StreamWriter (address + "\ Menu. ini ", true); // defines an object s. writeLine (openFileDialog1.FileName); // write the INI File s. flush (); // clears all the buffers of the current writer and writes all the buffered data to the base stream s. close (); // Close the current StreamWriter object and the basic stream ShowWindows (openFileDialog1.FileName ); // call the custom method ShowWindows} // read the INI file and add the information to the menu private void Form1_Load (object sender, EventArgs e) {StreamReader sr = new StreamReader (address + "\ Menu. ini "); // declare an object int I = this. file ToolStripMenuItem. dropDownItems. count-2; // defines an ini variable I and assigns it a while (sr. peek ()> = 0) // read the INI file {ToolStripMenuItem menuitem = new ToolStripMenuItem (sr. readLine (); // declare a ToolStripMenuItem object this. file ToolStripMenuItem. dropDownItems. insert (I, menuitem); // Add content I ++ to the menu; // The int variable I increments by menuitem. click + = new EventHandler (menuitem_Click); // generate a handler for the options in the menu} sr. close (); // Close the current StreamReader object and basic stream} private void menuitem_Click (object sender, EventArgs e) {ToolStripMenuItem menu = (ToolStripMenuItem) sender; ShowWindows (menu. text);} // custom method ShowWindows is used to load the background Image and display the form public void ShowWindows (string fileName) {Image p = Image. fromFile (fileName); // defines an Image Variable p Form f = new Form (); // defines a Form variable f. mdiParent = this; // set the current form to the MDI parent Form f. backgroundImage = p; // set the background image f for Form f. show (); // display form} private void exit ToolStripMenuItem_Click (object sender, EventArgs e) {Application. exit ();}}

002 menu dynamic merge

In C #4.0, the pop-up menu is encapsulated as the Context MenuStrip control. You can use the Items object in the control to operate the menu Items in the menu. This object is of the ToolStripMenuItem type. You can use the Item. AddRange method to add a menu Item to the pop-up menu.

Create a project. The default form is Form1. Add a MenuStrip control to the Form1 form to design the menu, and add a ContextMenuStrip control to the form to design the right-click menu. Add a form Form2 and add the ContextMenuStrip control to the form to design the right-click menu and set the Modifiers attribute to Public.

Namespace UniteMenu {public partial class Form1: Form {public Form1 () {InitializeComponent ();} private void toolstripmenuitem#click (object sender, EventArgs e) {Form2 f = new Form2 (); // define a Form object f. mdiParent = this; // set the MDI parent Form f of f. show (); // display sub-Form f. resize + = new EventHandler (f_Resize); // generates the adjusted handler for Form f} void f_Resize (object sender, EventArgs e) {Form2 f = (Form2) sender; // convert the sender type variable to Form type and assign it to f ToolStripMenuItem item = new ToolStripMenuItem (); // declare a ToolStripMenuItem object item for (int I = 0; I <f. contextMenuStrip2.Items. count;) // cyclically traverse each item in the menu {item. dropDownItems. add (f. contextMenuStrip2.Items [I]); // Add new content to the menu} this. contextMenuStrip1.Items. addRange (new System. windows. forms. toolStripItem [] {item}); // Add a set to the menu item }}}

003 beautiful menus like start menus

In C #4.0, the Child item ToolStripMenuItem in the MenuStrip control already contains the color bar on the left. It is very easy to implement a menu like the Start Menu. If you want to change the color bar on the left, you only need to set an image for the corresponding menu item.

Right-click the menu item (for example, Sava As) of the Image to be Set, select "Set Image...", and Import a local Image.

004 taskbar tray menu

To enable the program to appear in the system tray during startup, you must add the policyicon control and ContextMenuStrip control to the form, and set the Icon for the Icon attribute of the policyicon control.

1. Create a project and name it policyiconsl. The default form is Form1.

2. Add the policyicon control and ContextMenuStrip control to the Form1 form, and add sub-items for the ContextMenuStrip control.

3. Select the policyicon control, set the ContextMenuStrip property to the ContextMenuStrip control added to the form, and set the image for the Icon property.

005 menu interface that can be stretched

To implement a menu that can be stretched, you must use a switch variable and call the ShowDropDown method to display the operation results.

Create a project. The default form is Form1. Add a MenuStrip control to the Form1 form, select the MenuStrip control to add a subitem to it, and double-click expand (close) "Other items" add processing code for the click event.

Namespace _ 005_HideMenu {public partial class Form1: Form {int I = 2; public Form1 () {InitializeComponent ();} private void Form1_Load (object sender, EventArgs e) {// set the following menu items to hide this. set the password ToolStripMenuItem. visible = false; this. add User ToolStripMenuItem. visible = false; this. forgot password ToolStripMenuItem. visible = false; this. change the password ToolStripMenuItem. visible = false; this. employee input ToolStripMenuItem. visible = false;} private void expand close other items ToolStripMenuItem_Click (object sender, EventArgs e) {switch (I) {case 1: // set the following menu items to hide this. set the password ToolStripMenuItem. visible = false; this. add User ToolStripMenuItem. visible = false; this. forgot password ToolStripMenuItem. visible = false; this. change the password ToolStripMenuItem. visible = false; this. employee input ToolStripMenuItem. visible = false; I = 2; // display the ToolStripDropDownItem control related to ToolStripDropDownItem this. operate ToolStripMenuItem. showDropDown (); break; case 2: this. set the password ToolStripMenuItem. visible = true; this. add User ToolStripMenuItem. visible = true; this. forgot password ToolStripMenuItem. visible = true; this. change the password ToolStripMenuItem. visible = true; this. employee input ToolStripMenuItem. visible = true; I = 1; // display the ToolStripDropDownItem control related to ToolStripDropDownItem this. operate ToolStripMenuItem. showDropDown (); break ;}}}}

006 cascading menu

You must use the MenuStrip control to create a cascade menu.

: It is recommended that you use a cascade menu at no more than five layers. Otherwise, the user will feel inconvenient during use.

Create a project. The default form is Form1. Add the MenuStrip control in the Form1 form, and select the MenuStrip control to add subitems and cascade subitems to it.

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.