MDI with C #

xiaoxiao2021-03-06  86

MDI (Multiple Document Interface) is nothing but a way of displaying windows form where there is atleast one parent and many child windows eg. Word Excel Powerpoint kind of windows where each document, sheet or slide act as a child under the parent container window.

SDI (SINGLE Document Interface "Examples Are Windows Explorer, Internet Explorer, Notepad etc ... Where Only One Window Acts as an interface.

If you are a beginner (or intermediate) and want to develop an MDI application with basic functionality using the powerful C # language checkout the following step-by-step guide to develop. Even people coming from VB6 background face lots of problem because of the pure OOPS usage in c #. FOLLOW THIS SMALL Step by Step Walkthrough to make a small mdi application.

1> goto file-> new-> Blank Solution

2> SELECT Visual C # Projects in Project Types

3> SELECT WindowsApplication in Templates

4> Type for Eg. MyBestmdi in The Name TextBox

5> by Clicking the Browse Button Select The Location & Click OK

Creating All The Necessary Forms & Code Theme

==============================================

Creating The Main MDI Form (frmmdimain) to act as a container

-------------------------------------------------- ------------

1> In the view-> Solution Explorer Click Form1 Only Once

2> Locate The Property FileName and Type "frmmdimain.cs"

3> in The Solution Explorer Now DoubleClick frmmdimain

4> Locate The Property (Name) and type "frmmdimain"

5> Locate The Property Text and Type "this is the parent" 6> locate the property ismdicontainer and set it to true

7> Locate The Property WindowsTate and Set It To Maximized

Creating the child form (frmmchild) To Demonstrate Multiple Instance

-------------------------------------------------- ------------------

1> Goto Project Menu-Add Windows Forms & Type FRMMCHILD

2> In The Solution Explorer Now DoubleClick frmmchild

3> Locate The Property Text and Type "this is the multi instance lot"

4> Locate The Property Size & Set It To 568, 464

Creating the child form (frmschild) To Demonstrate Single Instance

-------------------------------------------------- ------------------

1> Goto Project Menu-Add Windows Forms & Type FRMSCHILD

2> In The Solution Explorer Now DoubleClick Frmschild

3> Locate The Property Text and Type "this is the single instance lot"

4> Locate The Property Size & Set It To 568, 464

Creating The About Form (frMabout) To Give Information About this Product

-------------------------------------------------- -----------------------

1> Goto Project Menu-Add Windows Forms & Type FRMabout

2> In The Solution Explorer Now DoubleClick Frmabout

3> Locate The Property Text and Type "About My Best MDI"

4> Locate The Property Size & Set It To 448, 304

5> Locate The Property FormbookTyle & Set It To FixedToolWindow

Creating The Splash Form (frmsplash) to Display The Welcome Information With Company Logo

-------------------------------------------------- -------------------------------------

1> Goto Project Menu-Add Windows Forms & Type FRMSPLASH

2> In the Solution Explorer Now DoubleClick frmsplash3> Locate The Property Size and Set It To 448, 320

4> Locate The Property ControlBox and Set It To False

5> Locate The Property FormrsTyle and set it to none

6> Locate The Property Startupposition and Set It To CenterUpposition and Set It To CenterScreen

7> Locate The Property BackgroundImage and Browse a Nice Image Along with your company logo

8> Locate The Property Cursor & Set It To Waitcursor

Adding a Timer Control to the Splash Screen for Holding It for Few Seconds So

That the user can read it.

-------------------------------------------------- -------------------------------------------

1> Goto View-> Toolbox and Double Click Timer Control

2> Locate Timer1 Control & Click It Once

3> SET ITS Interval Property to 2000 I.E. in MilliseConds

4> SET ITS Enabled Property to True

5> Double Click to Reach ITS Tick Event

TYPE: -

Timer1.enabled = false;

THIS.CLOSE ();

Adding Two Classes 1 To Act As The Main Startup Class (Clsmain) & 1 for Holding

Global Objects (Clsglobal)

-------------------------------------------------- -------------------------------------------

1> In The Solution Explorer Right Click The Project Name MyBestmdi

2> Click Add-> New Folder and Type Classes

3> Right Click The Folder Classes

4> Click Add-> Add New Item and Class and Type Clsglobal In Name

Note: - MyBestmdi.Classes Is Automatically Taken As The Namespace Due To Folder Level

5> Change the model for the class from

Public Class Clsglobal

TO

Public Sealed Class Clsglobal

(This is to protect an instantiation of this class)

6> Just after the curly braces of the class starts paste the following line

Public static frmmdimain g_objfrmmdimain; // this is to declare a global static reference to oor mdi parent so what the Same

// Instance is Referred Across All Child with no extra line of cot

7> Again in The Solution Explorer Right Click The Folder Classes

8> Click Add-> Add New Item and Class and Type CLSMain in Name

9> Just Below The Constructor Clsmain Function In The Clsmain Class Paste The Following

Snippet

[Stathread]

Static void main ()

{

Try

{

FRMSPLASH OBJFRMSPLASH = New frmsplash ();

Objfrmsplash.showdialog ();

Clsglobal.g_objfrmmdimain = new frmmdimain ();

Application.run (clsglobal.g_objfrmmdimain);

}

Catch (Exception EX)

{

Messagebox.Show (Ex.Message, "My Best MDI", MessageBoxButtons.ok, MessageBoxicon.Stop;

}

}

// this is The Single Threaded Apartment Model (Out of Our Scope) of the Application Which Will Run The Main Function

// when the application starts

10> Since We are for use application class and messagebox functions in the Above code We need a

Add The Foolowing Line on the top

Using system.windows.forms;

11> Now Go to frmmain now and from its code window remove the following piece of code

[Stathread]

Static void main ()

{

Application.run (New Form1 ());

}

// Because we cannot Have Two Main Function.we Are Invoking Everything from Clsmain

Creating Menus in The Main MDI Form

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

1> Click View-> Designer of frmmdimain

2> Goto Toolbox and Add A Double Click MainMenu Control

3> Click overce on the TypeHere Menu

4> Locate The Property Name & Set It to Mnufile

5> Locate The Property Text & Set It To & File

NOTE: - & Is for Underscore Which Will Enable The Hot Key 'F'

6> Same Way Click On The Sub Menu TypeHere Box7> Locate The Property Name & Set It To Mnufilenewmultiple

8> Locate The Property Text & Set It To & New Multiple

9> Locate The Property Shortcut & Choose Ctrln

10> Same Way Click on The Sub Menu TypeHere Box

11> Locate The Property Name & Set It To Mnufilenewsingle

12> Locate The Property Text & Set It to New & Single

13> Click on The Sub Menu TypeHere Box

14> Locate The Property Name & Set It To MnufileClosechild

15> Locate The Property Text & Set It To & Close Child

16> Click on The Sub Menu TypeHere Box

17> Locate The Property Name & Set It To MnuFilesep1

18> Locate The Property Text & Set It To -

Note - Is a Hyphen Which Will Automatically Put A Whole SEPERATOR

19> Click on The Sub Menu TypeHere Box

20> Locate The Property Name & Set It To MnufileExit

21> Locate The Property Text & Set It To E & XIT

Now I am Sure by Now you shouth becomle to create one more menu

22> Create One More Top Level Menu with Name Mnuwindow and Text & Window

Set ITS MDilist Property to True (Will Show Names of Opened Windows)

With 3 Sub Menus with Name

Mnuwindowcascade and text & cascade

MnuwindowtileVertical and text Tile & Vertical

Mnuwindowtilehorizontal and Text Tile & Horizontal

23> Create One More Top Level Menu with name MNUHELP AND TEXT & HELP

With a Sub Menu with Name MNUHELPABOUT AND TEXT & ABOUT ...

'...' as the suffix is ​​just a microsoft convention to signify That The Command Will Show A Dialog Box.

Putting life in the menus create by adding code to it

-------------------------------------------------- --------

1> NOW in the frmschild code address stat following linesprivate static frmschild m_schildform;

Public static frMschild getChildInstance ()

{

IF (m_schildform == null) // if not created YET, CREATE AN Instance

m_schildform = new frmschild ();

Return m_schildform; // just created or created earlier.return IT

}

// this is to make sure what WHEN WE CLICK A 'New Single' Menu on Parent Form Twice

// it Should Not Open TWO Instance of the Same Child Form

2> Click view-> Designer of frmmdimain again

3> Double Click New Single Menu ON MDI Main Form

Add The Following Code

FRMSCHILD OBJFRMSCHILD = frmsChild.getChildInstance ();

Objfrmschild.mdiparent = this;

Objfrmschild.show ();

Objfrmschild.bringtofront ();

4> Click view-> Designer of frmmdimain again

5> Double Click New Multi Menu ON MDI Main Form

Add The Following Code

FRMMCHILD OBJFRMMMMMMCHILD = New frmmchild ();

Objfrmmchild.mdiparent = this;

Objfrmmchild.show ();

6> Double Click Closechild On MDI Main Form

Add The Following Code

Try

{

IF (this.activemdichild.name == "frmmchild")

{

frMmchild ObjfrmmChild = (frmmchild) this.activemdichild;

Objfrmmchild.close ();

}

Else

{

FRMSCHILD OBJFRMSCHILD = (frmschild) this.activemdichild;

Objfrmschild.close ();

}

}

Catch

{

}

7> Double Click EXIT ON MDI MAIN FORM

Add The Following Code

Application.exit ();

8> Double Click Cascade Menu Under Windows ON MDI Main Form

Add The Following Code

This.Layoutmdi (MDILAYOUT.CASCADE);

9> Same Way for Tile Vertical Add

THIS.LAYOUTMDI (MDILAYOUT.TILEVERTICAL);

81> And for Tile Horizontal Add

THIS.LAYOUTMDI (MDILAYOUT.TILEHORIZONTAL);

20> Now Finally Double Click About Menu in Help To AddfrmAbout Objfrmabout = New FRMABOUT ();

Objfrmabout.showdialog ();

21> Press F5 To Start with Debugging OR Ctrl F5 to Start W / O Debugging

Njoi programing !!

The author (Irfan Patel) is a Microsoft Certified Solution Developer with Bachelor of Computer Application as the academic qualification. He has worked extensively with COM and has been programming since 1998. He has also trained more than 200 students and has taught various languages, RDBMS & platforms from C, C to VB with Oracle, Access & MS SQLServer from Vbscript, Javascript to ASP. His expertise comes from various industries viz Jewellery, Shipping, Automobile, Radio Frequency Devices, Photometers to name few. Besides programming he loves music, Singing, Dancing, Bike Riding & Cricket.

转载请注明原文地址:https://www.9cbs.com/read-122428.html

New Post(0)