MDI programming in Visual C #

Source: Internet
Author: User
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 ();
// Create a new form
Frmtemp. mdiparent = this;
// Define the parent form of the form, so that the form becomes an MDI form
Frmtemp. Text = "form 0" + formcount. tostring ();
// Set the title of the MDI form
Formcount ++;
Frmtemp. Show ();
// Display this MDI form
}
(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;
// Define this constant 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;
// This sentence is very important. 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 ();
// Create a new form
Frmtemp. mdiparent = this;
// Define the parent form of the form, so that the form becomes an MDI form
Frmtemp. Text = "form 0" + formcount. tostring ();
// Set the title of the MDI form
Formcount ++;
Frmtemp. Show ();
// Display this MDI form
}

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.

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.