Creating MDI Applications


So far, the applications that you have developed using Windows Forms are what are known as Single Document Interface (SDI) applications, which means that you are working with one underlying Windows instance. Multiple Document Interface (MDI) applications provide a model in which an application can contain multiple instances of child windows , all contained in the parent window. For an easier understanding of MDI applications, compare Notepad with Word. Whereas Notepad allows only one document to be edited at one time, Word (or any other Office application) allows multiple windows to be opened at one time.

Windows Forms applications can be MDI enabled by setting the IsMdiContainer property to true. After the base form is MDI enabled, multiple instances of child forms can be instantiated and assigned to the parent window by assigning the MdiParent property to the parent window.

For instance, consider Listing 7.10. This simple 84-line program provides almost a complete implementation of a basic editor application (similar to Notepad; although file-save functionality is not yet implemented, Figure 7.14). An interesting capability here is that of menu merging. Menu merging provides the capability to merge the menu items of the parent window with the child window to create a richer and more intuitive user experience. The MergeOrder property controls the exact location within the various menu items where the merge takes place.

Listing 7.10 An MDI Application
 using System; using System.Windows.Forms; namespace MyCompany {  namespace MyApp  {    class Editor : Form {    private int docId=0;    public Editor()    {       this.Text = "Editor";       this.IsMdiContainer = true;       MainMenu menu = new MainMenu();       MenuItem fileMenu = new MenuItem("&File");           fileMenu.MergeOrder=0;                fileMenu.MergeType = MenuMerge.MergeItems;       MenuItem newMenuItem = new MenuItem("&New");       newMenuItem.MergeOrder = 1;       MenuItem exitMenuItem = new MenuItem("E&xit");       exitMenuItem.MergeOrder = 3;       fileMenu.MenuItems.Add(newMenuItem);       fileMenu.MenuItems.Add(exitMenuItem);       menu.MenuItems.Add(fileMenu);       MenuItem helpMenu = new MenuItem("&Help");       MenuItem helpTopicsMenuItem = new MenuItem("&Help Topics");       MenuItem aboutMenuItem = new MenuItem("&About");       helpMenu.MenuItems.Add(helpTopicsMenuItem);       helpMenu.MenuItems.Add("-");       helpMenu.MenuItems.Add(aboutMenuItem);       menu.MenuItems.Add(helpMenu);       this.Menu = menu;       newMenuItem.Click += new EventHandler(this.NewMenuItem_Click);       exitMenuItem.Click += new EventHandler(this.ExitMenuItem_Click);       aboutMenuItem.Click += new EventHandler(this.AboutMenuItem_Click);    }    protected void ExitMenuItem_Click(Object sender, EventArgs e)    {       Application.Exit();    }    protected void AboutMenuItem_Click(Object sender, EventArgs e)    {       MessageBox.Show("Simple Editor by Hitesh Seth. All Rights Reserved.",          "Simple Editor v1.0",           MessageBoxButtons.OK, MessageBoxIcon.Exclamation);    }    protected void NewMenuItem_Click(Object sender, EventArgs e)    {       docId = docId+1;       EditorDoc editorDoc = new EditorDoc("Document #"+docId);       editorDoc.MdiParent = this;       editorDoc.Show();    }    public static void Main()    {       Editor editor = new Editor();       Application.Run(editor);    }    }    public class EditorDoc : Form {    private RichTextBox textBox;    public EditorDoc(String name)    {       this.Text = name;       textBox = new RichTextBox();       textBox.Dock = DockStyle.Fill;       Controls.Add(textBox);       MainMenu menu = new MainMenu();       MenuItem fileMenu = new MenuItem("&File");           fileMenu.MergeOrder=0;                fileMenu.MergeType = MenuMerge.MergeItems;       MenuItem saveMenuItem = new MenuItem("&Save");       saveMenuItem.MergeOrder=2;       fileMenu.MenuItems.Add(saveMenuItem);       menu.MenuItems.Add(fileMenu);       this.Menu = menu;    }    }  } } 
Figure 7.14. MDI Editor application.



Microsoft.Net Kick Start
Microsoft .NET Kick Start
ISBN: 0672325748
EAN: 2147483647
Year: 2003
Pages: 195
Authors: Hitesh Seth

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net