MDI With C#

来源:互联网 发布:php关联数组遍历 编辑:程序博客网 时间:2024/04/30 05:55

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 Solution2> Select Visual C# Projects in Project Types3> Select WindowsApplication in Templates4> Type for eg. myBestMDI in the Name textbox5> By Clicking the browse button select the location & Click OKCREATING ALL THE NECESSARY FORMS & CODE THEM============================================Creating the main MDI form(frmMDIMain) to act as a container------------------------------------------------------------1> In the View->Solution Explorer click Form1 only once2> 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 true7> Locate the property WindowState and set it to MaximizedCreating the child form (frmMChild) to demonstrate multiple instance--------------------------------------------------------------------1> Goto Project Menu-Add Windows Forms & type frmMChild2> In the Solution Explorer now doubleclick frmMChild3> Locate the property Text and type "This is the Multi Instance Child"4> Locate the property size & set it to 568, 464Creating the child form (frmSChild) to demonstrate Single instance--------------------------------------------------------------------1> Goto Project Menu-Add Windows Forms & type frmSChild2> In the Solution Explorer now doubleclick frmSChild3> Locate the property Text and type "This is the Single Instance Child"4> Locate the property size & set it to 568, 464Creating the About form (frmAbout) to give information about this product-------------------------------------------------------------------------1> Goto Project Menu-Add Windows Forms & type frmAbout2> In the Solution Explorer now doubleclick frmAbout3> Locate the property Text and type "About My Best MDI"4> Locate the property size & set it to 448, 3045> Locate the property FormBorderstyle & set it to FixedToolWindowCreating the Splash form (frmSplash) to display the welcome information with company logo---------------------------------------------------------------------------------------1> Goto Project Menu-Add Windows Forms & type frmSplash2> In the Solution Explorer now doubleclick frmSplash3> Locate the property size and set it to 448, 3204> Locate the property ControlBox and set it to False5> Locate the property FormBoderStyle and set it to None6> Locate the property StartupPosition and set it to CenterScreen7> Locate the property BackGroundImage and browse a nice image along with your company logo8> Locate the property cursor & set it to waitCursorAdding 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 control2> Locate timer1 control & click it once3> Set its Interval property to 2000 i.e. in milliseconds4> Set its Enabled property to true5> Double click to reach its Tick eventtype :- timer1.Enabled = false;this.Close();Adding two classes 1 to act as the Main startup class(clsMain) & 1 for holdingGlobal objects(clsGlobal)---------------------------------------------------------------------------------------------1> In the Solution explorer right click the Project Name myBestMdi2> Click Add->New Folder and type classes3> right click the Folder classes4> Click Add->Add New Item and class and type clsGlobal in nameNote:- myBestMDI.Classes is automatically taken as the namespace due to folder level5> change the modifiers 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 linepublic static frmMDIMain g_objfrmMDIMain;//this is to declare a global static reference to our MDI parent so that the same //instance is referred across all child with no extra line of code7> Again in the Solution explorer right click the Folder classes8> Click Add->Add New Item and class and type clsMain in name9> 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 starts10> Since we are using Application class and MessageBox functions in the above code we need a add the foolowing line on the topusing 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 clsMainCreating menus in the main MDI form--------------------------------------1> Click View->Designer of frmMdiMain2> Goto Toolbox and add a double click MainMenu control3> Click once on the typehere menu 4> Locate the property Name & set it to mnuFile5> Locate the property Text & set it to &FileNote:- & 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 mnuFileNewMultiple8> Locate the property Text & set it to &New Multiple9> Locate the property ShortCut & choose CtrlN10> Same way click on the sub menu TypeHere box11> Locate the property Name & set it to mnuFileNewSingle12> Locate the property Text & set it to New &Single13> click on the sub menu TypeHere box14> Locate the property Name & set it to mnuFileCloseChild15> Locate the property Text & set it to &Close Child16> click on the sub menu TypeHere box17> Locate the property Name & set it to mnuFileSep118> Locate the property Text & set it to -  Note - is a hyphen which will automatically put a whole seperator19> click on the sub menu TypeHere box20> Locate the property Name & set it to mnuFileExit21> Locate the property Text & set it to E&xitNow i am sure by now you should be able to create one more menu22> Create one more top level menu with Name mnuWindow and text &Windowset its MDIList property to true(will show names of opened windows)with 3 sub menus with name mnuWindowCascade and text &CascademnuWindowTileVertical and text Tile&VerticalmnuWindowTileHorizontal and text Tile&Horizontal23> Create one more top level menu with Name mnuHelp and text &Helpwith 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 created by adding code to it--------------------------------------------------------1> Now in the frmSChild code add the following lines  private 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 that when we Click on a 'New Single' menu on Parent form twice //it should not open two instance of the same child form2> Click View->Designer of frmMdiMain again3> Double click New Single Menu on MDI main formadd the following codefrmSChild objfrmSChild = frmSChild.GetChildInstance();objfrmSChild.MdiParent = this;objfrmSChild.Show();objfrmSChild.BringToFront();4> Click View->Designer of frmMdiMain again5> Double click New Multi Menu on MDI main formadd the following codefrmMChild objfrmMChild = new frmMChild();objfrmMChild.MdiParent = this;objfrmMChild.Show();6> Double click CloseChild on MDI main formadd the following codetry{ 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 formadd the following codeApplication.Exit(); 8> Double click Cascade menu under Windows on MDI main formadd the following codethis.LayoutMdi(MdiLayout.Cascade);9> same way for tile Vertical add this.LayoutMdi(MdiLayout.TileVertical);81> and for tile horizontal addthis.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 debuggingNjoi programming!!
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.
原创粉丝点击