C # instances: 5. NET classic examples (form and interface design)

Source: Internet
Author: User
Tags read ini file

Example 001 menu with historical information

Example description

When developing the drawing management software, it is required to record the user's recently opened files or drawings on the menu to facilitate the next use. 1.1, click the Open File submenu under the File menu to open the sheet you want to view. The next time you run the software, the last opened file name is recorded in the History menu of the File menu, and the corresponding sheet file is opened by selecting the menu.

Technical Essentials

To implement saving the most recently opened file, you can save the file name and path of the most recently opened files in the menu to a pre-established *.ini file, and when the system starts reading the data from the *.ini, an array menu is available to display the function of the history menu.

Note: To create a menu with historical information, you must first add a MenuStrip menu control and set the main form's IsMdiContainer property to True.

Implementation process

(1) Create a project, name it ex01_01, and the default form is Form1.

(2) Add the MenuStrip control to the Form1 form from the Toolbox and add the OpenFileDialog control to the form. Create a "file" main menu under which you create a menu option that opens, closes all, exits, and so on.

(3) Main program code.

The implementation code that writes the open file path to the INI file is as follows:

private void Open Toolstripmenuitem_click (object sender, EventArgs e)

{

Openfiledialog1.filename = "";

This.openFileDialog1.ShowDialog ();

StreamWriter s = new StreamWriter (address + "\\Menu.ini", true);

S.writeline (openfiledialog1.filename);//write INI file

S.flush ();

S.close ();

Showwindows (Openfiledialog1.filename);

}

The implementation code for reading the INI file and adding the information to the menu is as follows:

private void Form1_Load (object sender, EventArgs e)

{

StreamReader sr = new StreamReader (address + "\\Menu.ini");

int i = this. File toolstripmenuitem.dropdownitems.count-2;

while (Sr. Peek () >=0)//Read INI file

{

ToolStripMenuItem MenuItem = new ToolStripMenuItem (Sr.) ReadLine ());

this. File ToolStripMenuItem.DropDownItems.Insert (I, MenuItem);

i++;

MenuItem. Click + = new EventHandler (Menuitem_click);

}

Sr. Close ();

}

The custom Method Showwindows () is used to load the background picture and display the form, implementing the following code:

public void Showwindows (string fileName)

{

Image p = image.fromfile (fileName);

Form f = new form ();

F.mdiparent = this;

F.backgroundimage = p;

F.show ();

}

Extrapolate

According to this example, the reader can develop the following programs.

The program that records the user's Action menu log. When the user clicks the menu, the user, menu commands, and menu functions are written to the INI file where the menu log is saved. If you need to view the log, simply open the INI file.

A program that saves menu history information through a database.

The program that uses the frequency of the menu. Save the user's data from the menu to the database, and then count how often the user uses the menu, and adjust the order in which the menus are displayed, based on this frequency.

Instance 002 Menu Dynamic Merge

Example 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 the MDI child forms are maximized, the menus of the subform and main form can be automatically merged. How is this implemented? This example implements the ability to dynamically merge two popup menus into a pop-up menu. The instance effect is shown in 1.2.

Technical Essentials

In C # 2.0, the pop-up menu has been encapsulated as a context MenuStrip control, with the items object in the control to manipulate menu items in the menu. The object is of type ToolStripMenuItem, using the Items.addrange () method to add a menu item to the pop-up menu, which is prototyped as follows.

public void AddRange (

Toolstripitem[] Toolstripitems

)

The parameters are described below.

L Toolstripitems: An array of controls.

Implementation 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, add ContextMenuStrip controls to the form to design the context menu, and select the MenuStrip control to create a main open subform menu, Then select the ContextMenuStrip control to add children to it.

(3) Add a form to the program, the default name is Form2, add the ContextMenuStrip control to the form to design the right-click menu, and then select the ContextMenuStrip control to add children to it.

(4) Main program code.

private void open from Form Toolstripmenuitem_click (object sender, EventArgs e)

{

Form2 f= new Form2 ();

F.mdiparent = this;

F.show ();//Show Subform

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});

}

Extrapolate

Based on this example, the reader can implement the following functions.

Make the right-click menu appear in the subform.

Make the right-click menu appear in both the main form and the subform.

Example 003 Menu as beautiful as the Start menu

Example description

The Start menu of Windows is unique, with a vertical color bar next to the menu, and text in the color bar. This unique menu can make the interface of the program look more beautiful. In this example, the menu is implemented, and when you run this example, the Open menu pops up and you see a purple color bar on the left side of the menu. The instance effect is shown in 1.3.

Technical Essentials

In C # 2.0, the subkey ToolStripMenuItem in the MenuStrip control already includes the color bar on the left, which is easy to implement like the Start menu, unlike in the other computer language development environment, which requires the calling API to be implemented. If you want to change the left vertical color bar, just give the corresponding menu item to set the corresponding picture.

Note: If you want to display text on the left color bar, simply add text to the corresponding image.

Implementation process

(1) Create a project, name it ex01_03, and the default form is Form1.

(2) Add the MenuStrip control from the toolbox to the Form1 form.

(3) Add the appropriate subkey for the MenuStrip control.

(4) Add the appropriate picture for the child.

Extrapolate

Based on this example, the reader can implement the following functions.

Set the menu element to a different format (slices, text, and so on).

Play the animation on the left side of the menu.

Example 004 Taskbar tray Menu

Example description

Some software is typically run in the background, and these processes do not display the user interface most of the time. A virus protection program that you can access by clicking the icon in the status notification area of the taskbar is an example. The NotifyIcon control in Windows Forms is typically used to display the icon for a process running in the background, and this example makes use of the control to create a tray menu for the taskbar. The instance effect is shown in 1.4.

Technical Essentials

Appears in the system tray to implement program startup. You must add NotifyIcon controls and ContextMenuStrip controls to the form.

Note: You must set the icon for the NotifyIcon control's Icon property.

Implementation process

(1) Create a project, name it ex01_04, and the default form is Form1.

(2) Add NotifyIcon controls and ContextMenuStrip controls to the Form1 form and add children to the ContextMenuStrip control.

(3) Select the NotifyIcon control, set the ContextMenuStrip property in its Properties window to add to the ContextMenuStrip control on the form, and set a picture for the Icon property.

Extrapolate

According to this example, the reader can develop the following programs.

The program does not appear at the start of the interface, directly appear in the system tray running background program.

The program does not start when it is in the taskbar.

Example 005 a menu interface that can be stretched

Example description

If the hypervisor features menus are very numerous and users only use some of the common menus, you can hide the infrequently used menus under the main menu item. This type of display is similar to stretching the menu. When you use it, simply click the Expand menu to display the appropriate menu features. Run this example, as shown in effect 1.5.

Technical Essentials

To implement a menu that can be stretched, the key is to use a switch variable and call the Showdropdown () method to display the result after the operation. The method is described in more detail below.

The Showdropdown () method is used to display the Toolstripdropdownitem control associated with this toolstripdrop Downitem. The syntax structure is as follows:

public void Showdropdown ()

In addition, the Showdropdown () method can also display a drop-down control that has been set by the DropDown property.

Note: The initial value of the switch variable must be set.

Implementation process

(1) Create a project, name it ex01_05, and the default form is Form1.

(2) From the Toolbox, add the MenuStrip control to the Form1 form, and select the MenuStrip control to add children to it.

(3) Double-click "Expand (Close) subkey" To add processing code for its double-click event.

(4) Main program code.

private void Form1_Load (object sender, EventArgs e)

{

The menu below the initial settings is hidden

this. Set password toolstripmenuitem.visible = false;

this. Add user Toolstripmenuitem.visible = false;

this. Forgot password toolstripmenuitem.visible = false;

this. Change password toolstripmenuitem.visible = false;

this. Employee input toolstripmenuitem.visible = false;

}

private void Toolstripmenuitem1_click (object sender, EventArgs e)

{

Switch (i)

{

Case 1:

this. Set password toolstripmenuitem.visible = false;

this. Add user Toolstripmenuitem.visible = false;

this. Forgot password toolstripmenuitem.visible = false;

this. Change password toolstripmenuitem.visible = false;

this. Employee input toolstripmenuitem.visible = false;

i = 2;

this. Operation Toolstripmenuitem.showdropdown ();

Break

Case 2:

this. Set Password toolstripmenuitem.visible = true;

this. Add user Toolstripmenuitem.visible = true;

this. Forgot password toolstripmenuitem.visible = true;

this. Change Password toolstripmenuitem.visible = true;

this. Employee input toolstripmenuitem.visible = true;

i = 1;

this. Operation Toolstripmenuitem.showdropdown ();

Break

}

}

Extrapolate

Based on this example, the reader can develop the following functions.

Make show \ Hide toolbar.

Merges the menu bar.

C # instances: 5. NET classic examples (form and interface design)

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.