MDI programming in Visual C #

Source: Internet
Author: User

MDI programming in Visual C #

The multi-document interface is the so-called MDI, which was introduced from the Microsoft Excel spreadsheet program in Windows 2.0. This is because the Excel spreadsheet user sometimes needs to operate multiple tables at the same time, MDI provides great convenience for such operations on multiple tables, so an MDI program is generated. In Windows 3.1, MDI has been widely used. The program manager and file manager in the system are both MDI programs.
Visual C # is the next-generation mainstream programming language launched by Microsoft. It is also a very powerful programming language and is becoming popular with more and more programmers. In Visual C #, many functions are provided to implement the MDI program design. This article describes the MDI programming in Visual C # through a specific example.

A program design and running environment:

(1) Windows 2000 Server Edition
(2). Net FrameWork SDK Beta 2

Ii. Ideas, main steps and implementation methods of Program Design:

MDI programming is mainly to create an MDI form in the main form, and to cascade, horizontally tile, and vertically tile all the MDI forms in the main form. Although these operations are basic, they are the key points and key points in programming. This article describes the functions in the order described above.

(1) first, you must set the main form to be a container of the MDI form, because only in this way can you add the MDI form to the main form to implement the MDI programming. The specific implementation statement is as follows:
This. IsMdiContainer = true;

(2) create an MDI form on the main form. In the program, the command to create an MDI form is implemented through a menu event. In the process of creating an MDI form event, the key is to set the parent form of the MDI form. In fact, there is no difference between the MDI form and other forms. The difference lies in that the MDI form has a upper-level form, that is, the parent form, while other forms do not. The specific implementation statement is as follows:
Private void New_Click (object sender, EventArgs e) // This is a menu event
{
Form frmTemp = new Form ();
File: // newCreate a form
FrmTemp. MdiParent = this;
File: // setDefine the parent form of the form, so that the form becomes an MDI form
FrmTemp. Text = "form 0" + FormCount. ToString ();
File: // setDetermine the title of the MDI form
FormCount ++;
FrmTemp. Show ();
File: //This MDI form is displayed
}
(3) cascade the MDI form:

For the cascade operation on the MDI form in the main form, it is implemented through a method in the main program. This method is LayoutMdi, and its parameter is MdiLayout. cascade:
Private void Cascade_Click (object sender, EventArgs e)
// Cascade the MDI form in the main form
{
This. LayoutMdi (MdiLayout. Cascade );
}

(4) Horizontal Tile of the MDI form:

To horizontally tile the MDI form in the main form, the LayoutMdi method is also used. The parameter is MdiLayout. TileHorizontal. The specific implementation statement is as follows:

Private void TileH_Click (object sender, EventArgs e)
// Implements horizontal tiled operation on the MDI form in the main form
{
This. LayoutMdi (MdiLayout. TileHorizontal );
}

(5) Implement vertical Tile of the MDI form:

To vertically tile the MDI form in the main form, the LayoutMdi method is also used. The parameter is MdiLayout. TileVertical. The specific implementation statement is as follows:

Private void TileV_Click (object sender, EventArgs e)
// Implements vertical tiled operation on the MDI form in the main form
{
This. LayoutMdi (MdiLayout. TileVertical );
}

(6) In some MDI programs, after an MDI form is created, a sub-menu item named by the name of this MDI form is often produced under some menu items. To implement this function in other languages, you may need to dynamically Add sub-Menus under some main menu items. However, implementing this function in Visual C # is relatively simple. You only need to add the following program under the main menu to be added:
WindowMenu. MdiList = true; in this program, the Child menu item of the MDI form is added under the "window" main menu item.

3. program source code (mdi. cs) and compilation method:

After introducing the difficulties in programming, you can easily obtain the source code (MDI. cs) for processing the aforementioned mdi form, as shown below:

Mdi. cs source code:

Using System;
Using System. Windows. Forms;
Using System. ComponentModel;
Using System. Drawing;
// The namespace used for import in the program
Class MDIDemo: Form
{
Private static int FormCount = 1;
File: // setThis constant is used to count the number of MDI forms,
MainMenu mnuMain = new MainMenu ();
MenuItem FileMenu;
MenuItem NewMenu;
MenuItem ExitMenu;
MenuItem WindowMenu;

Public MDIDemo ()
{
This. IsMdiContainer = true;
This. Text = "MDI Demo ";
FileMenu = new MenuItem ();
FileMenu. Text = "file ";
WindowMenu = new MenuItem ();
WindowMenu. Text = "window (& W )";
WindowMenu. MenuItems. Add ("form cascade (& C)", new EventHandler (Cascade_Click ));
WindowMenu. MenuItems. Add ("Horizontal tile (& H)", new EventHandler (TileH_Click ));
WindowMenu. MenuItems. Add ("vertical tile (& V)", new EventHandler (TileV_Click ));
WindowMenu. MdiList = true;
File: // ThisAn important sentence. With this sentence, you can create an MDI form and display the existing MDI form menu items under this main menu item.

NewMenu = new MenuItem ();
NewMenu. Text = "New Form (& N )";
NewMenu. Click + = new EventHandler (New_Click );

ExitMenu = new MenuItem ();
ExitMenu. Text = "exit (& X )";
ExitMenu. Click + = new EventHandler (Exit_Click );

FileMenu. MenuItems. Add (NewMenu );
FileMenu. MenuItems. Add (new MenuItem ("-"));
FileMenu. MenuItems. Add (ExitMenu );

MnuMain. MenuItems. Add (FileMenu );
MnuMain. MenuItems. Add (WindowMenu );

This. Menu = mnuMain;
}

Private void Cascade_Click (object sender, EventArgs e)
// Cascade the MDI form in the main form
{
This. LayoutMdi (MdiLayout. Cascade );
}

Private void TileH_Click (object sender, EventArgs e)
// Implements horizontal tiled operation on the MDI form in the main form
{
This. LayoutMdi (MdiLayout. TileHorizontal );
}

Private void TileV_Click (object sender, EventArgs e)
// Implements vertical tiled operation on the MDI form in the main form
{
This. LayoutMdi (MdiLayout. TileVertical );
}


Private void New_Click (object sender, EventArgs e)
{
Form frmTemp = new Form ();
File: // newCreate a form
FrmTemp. MdiParent = this;
File: // setDefine the parent form of the form, so that the form becomes an MDI form
FrmTemp. Text = "form 0" + FormCount. ToString ();
File: // setDetermine the title of the MDI form
FormCount ++;
FrmTemp. Show ();
File: //This MDI form is displayed
}

Private void Exit_Click (object sender, EventArgs e)
{
This. Dispose ();
Application. Exit ();
}
Public static void Main ()
{
Application. Run (new MDIDemo ());
}
}

After the following compilation commands are compiled, you can get the execution program:
Csc/t: winexe/r: system. dll/r: system. windows. forms. dll/r: system. drawing. dll mdi. cs

Iv. Summary:

This article describes how to use Visual C # To program an MDI form. That is, create an MDI form, an MDI form cascade, an MDI form horizontal tile, and an MDI form vertical tile. Through the above introduction, we can see that it is quite simple to process MDI in Visual C #.

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.