C # example: 5. net classic examples (Form and interface design ),

Source: Internet
Author: User

C # example: 5. net classic examples (Form and interface design ),

Instance 001 menu with historical information

Instance description

When developing the drawing management software, you must record the files or drawings recently opened by the user on the menu to facilitate the next use. 1.1. Click the "open file" sub-menu under the "file" menu to open the drawing to be viewed. The next time you run the software, the last opened file name is recorded in the history menu of the "file" menu. Select this menu to open the corresponding drawing file.

Technical Points

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 system reads the file at startup *. you can create an array menu for data in ini to display the history menu.

Note: to create a menu with historical information, you must first add a MenuStrip Menu Control and set the IsMdiContainer attribute of the main form to True.

Implementation Process

(1) create a project named Ex01_01. 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 menu options such as opening, closing, and exiting are created.

(3) Main program code.

The following code writes the open file path to the INI file:

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 the INI File

S. Flush ();

S. Close ();

ShowWindows (openFileDialog1.FileName );

}

The following code reads the INI file and adds the information to the menu:

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 the 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 image and display the form. The implementation code is as follows:

Public void ShowWindows (string fileName)

{

Image p = Image. FromFile (fileName );

Form f = new Form ();

F. MdiParent = this;

F. BackgroundImage = p;

F. Show ();

}

Let alone

Based on this instance, you can develop the following programs.

The program that records user operation menu logs. When a user clicks a menu, the user, menu commands, and corresponding menu functions are written into the INI file that saves the menu log. To view logs, open the INI file.

The program that saves menu history information through the database.

Menu frequency program. Save the data information of user menus to the database, calculate the frequency of user menus, and adjust the display sequence of menus according to the frequency.

Dynamic merge of instance 002 menu

Instance description

Pop-up menus are often used in programs, and multiple pop-up menus can exist in one form. Readers who have developed an MDI form may know that when the MDI child form is maximized, the menus of the child form and the main form can be automatically merged. How is this implemented? In this example, the two pop-up menus are dynamically merged into a pop-up menu. The instance effect is 1.2.

Technical Points

In C #2.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 Items. AddRange () 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 as follows.

L toolStripItems: array of controls.

Implementation Process

(1) create a project named Ex01_02. The default form is Form1.

(2) Add a MenuStrip control to Form1 form from the toolbox to design the menu, and add the ContextMenuStrip control to the form to design the right-click menu. Select the MenuStrip control to create a "Open subform" main menu, select the ContextMenuStrip control to add a subitem to it.

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

(4) main program code.

Private void open self-form ToolStripMenuItem_Click (object sender, EventArgs e)

{

Form2 f = new Form2 ();

F. MdiParent = this;

F. Show (); // display subforms

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

}

Let alone

Based on this instance, you can implement the following functions.

Right-click the menu to display it in the subform.

The right-click menu is displayed in both the main form and sub-form.

Instance 003 is as beautiful as the Start Menu

Instance description

Windows's Start Menu is very unique. There is a vertical bar next to the menu, and the color bar also contains text. This unique menu makes the interface of the program look more beautiful. This menu is implemented in this example. When "Open menu" is displayed in this example, a purple colored bar is displayed on the left of the menu. The instance effect is 1.3.

Technical Points

In C #2.0, the sub-item ToolStripMenuItem in the MenuStrip control already contains the color bar on the left. It is very easy to implement menus like the Start Menu, unlike in the development environment of other computer languages, you must call an API to implement this function. To change the vertical bar on the left, you only need to set the corresponding picture for the corresponding menu item.

Note: To display text in the left-side color bar, you only need to add text to the corresponding image.

Implementation Process

(1) create a project named Ex01_03. The default form is Form1.

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

(3) Add a child item for the MenuStrip control.

(4) add an image for the subitem.

Let alone

Based on this instance, you can implement the following functions.

Set Menu elements to different formats (slice and text ).

Play the animation on the left side of the menu.

Instance 004 taskbar tray menu

Instance description

Some software usually runs in the background, and these processes do not display the user interface most of the time. The virus protection program that can be accessed by clicking the icon in the notification area of the taskbar status is an example. The yyicon control in Windows Forms is usually used to display the icons of processes running in the background. This example uses this control to create a taskbar tray menu. The instance effect is 1.4.

Technical Points

To enable the program to appear in the system tray during startup. You must add the policyicon control and ContextMenuStrip control to the form.

Note: The Icon must be set for the Icon attribute of the policyicon control.

Implementation Process

(1) create a project named Ex01_04. 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 yyicon control, set the ContextMenuStrip attribute in Its Properties window to the ContextMenuStrip control added to the form, and set the image for the Icon attribute.

Let alone

Based on this instance, you can develop the following programs.

When the program starts, there is no interface, and the background program that runs in the system tray directly appears.

The program cannot be started in the taskbar.

Menu interface that can be stretched for instance 005

Instance description

If you only use some common menus, you can hide the uncommon Menus under the main menu items. This display method is similar to stretching a menu. When using this function, you only need to click expand menu to display the corresponding menu function. Run this example. The result is 1.5.

Technical Points

To implement a menu that can be stretched, you must use a switch variable and call the ShowDropDown () method to display the operation result. This method is described in 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, you can use the ShowDropDown () method to display the drop-down control that has been set by the DropDown attribute.

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

Implementation Process

(1) create a project named Ex01_05. The default form is Form1.

(2) Add the MenuStrip control to the Form1 form from the toolbox, and select the MenuStrip control to add a subitem to it.

(3) double-click "Expand (close) subitem" to add processing code for its double-click event.

(4) main program code.

Private void Form1_Load (object sender, EventArgs e)

{

// Initially set the following menu 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 toolstripmenuitem#click (object sender, EventArgs e)

{

Switch (I)

{

Case 1:

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;

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;

This. Operate ToolStripMenuItem. ShowDropDown ();

Break;

}

}

Let alone

Based on this instance, you can develop the following functions.

Create the display \ hide toolbar.

Merge menu bar.


C language ^ how to use

A1 = 0x01; // 0000 0001
A2 = 0x00; // 0000 0000
A3 = 0x03; // 0000 0011
A4 = 0x02; // 0000 0010

B1 = a1 ^ a2; // 0000 0001
B2 = a1 ^ a3; // 0000 0010
B3 = a1 ^ a4; // 0000 0011

^ XOR operator. The bitwise value is 0 and the difference is 1. See the example above.

//
Examples of simple and practical problems:
====================================
======= A ======= B =========
There are two circuits on the top. The two switches are a and B respectively. The opening status is \ [1], and the closing status is/[0].
If both circuits are enabled or disabled.
If a turns on [1], B turns off [0], and circuit 1 Powers on
=====================
If a disables [0], B enables [1], and circuit 2 powers on.
====================================
In summary, the circuit fails in the and B states simultaneously [0]. When a and B are different, the power is charged [1].

C language ^ how to use

A1 = 0x01; // 0000 0001
A2 = 0x00; // 0000 0000
A3 = 0x03; // 0000 0011
A4 = 0x02; // 0000 0010

B1 = a1 ^ a2; // 0000 0001
B2 = a1 ^ a3; // 0000 0010
B3 = a1 ^ a4; // 0000 0011

^ XOR operator. The bitwise value is 0 and the difference is 1. See the example above.

//
Examples of simple and practical problems:
====================================
======= A ======= B =========
There are two circuits on the top. The two switches are a and B respectively. The opening status is \ [1], and the closing status is/[0].
If both circuits are enabled or disabled.
If a turns on [1], B turns off [0], and circuit 1 Powers on
=====================
If a disables [0], B enables [1], and circuit 2 powers on.
====================================
In summary, the circuit fails in the and B states simultaneously [0]. When a and B are different, the power is charged [1].

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.