Click here to Skip to main content
15,886,199 members
Articles / Programming Languages / C#

C# SDI/MDI Application Wizards

Rate me:
Please Sign up or sign in to vote.
3.50/5 (28 votes)
30 Nov 20038 min read 226.5K   2.7K   60  
Useful project templates for C# document-centric applications
In this article, I'll show what is brought in the zip files, how to install the application wizard, the meaning of SDI/MDI and how it was built
//Copyright (C) 2000 Microsoft Corporation.  All rights reserved.

//This source code is intended only as a supplement to Microsoft
//Development Tools and/or on-line documentation.  See these other
//materials for detailed information regarding Microsoft code samples.

//THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
//KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
//IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
//PARTICULAR PURPOSE.

namespace [!output SAFE_NAMESPACE_NAME]
{
using System;
using System.Drawing;
using System.Drawing.Printing;

using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;


/// <summary>
///    Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
	/// <summary> 
	///    Required designer variable
	/// </summary>
	/// 

	public static Form1 parentWindow;
	private System.ComponentModel.IContainer components;
	private System.Windows.Forms.StatusBar statusBar1;
	private System.Windows.Forms.ImageList imageList1;
	private System.Windows.Forms.ToolBarButton printButton;
	private System.Windows.Forms.ToolBarButton previewButton;
	private System.Windows.Forms.ToolBarButton saveButton;
	private System.Windows.Forms.ToolBarButton openButton;
	private System.Windows.Forms.ToolBarButton newButton;
	private System.Windows.Forms.ToolBar toolBar1;
	private System.Drawing.Printing.PrintDocument printDoc;
		
	private System.Windows.Forms.MenuItem menuItemAbout;
	private System.Windows.Forms.MenuItem menuItemTile;
	private System.Windows.Forms.MenuItem menuItemCascade;
	private System.Windows.Forms.MenuItem menuItemNewWindow;
	private System.Windows.Forms.MenuItem menuItemStatusbar;
	private System.Windows.Forms.MenuItem menuItemToolbar;
	private System.Windows.Forms.MenuItem menuItemClearAll;
	private System.Windows.Forms.MenuItem menuItemExit;
	private System.Windows.Forms.MenuItem menuItem16;
	private System.Windows.Forms.MenuItem menuItemPreview;
	private System.Windows.Forms.MenuItem menuItemPrint;
	private System.Windows.Forms.MenuItem menuItem12;
	private System.Windows.Forms.MenuItem menuItemSaveAs;
	private System.Windows.Forms.MenuItem menuItemSave;
	private System.Windows.Forms.MenuItem menuItemClose;
	private System.Windows.Forms.MenuItem menuItemOpen;
	private System.Windows.Forms.MenuItem menuItemNew;
	private System.Windows.Forms.MenuItem menuItemHelp;
	private System.Windows.Forms.MenuItem menuItemWindow;
	private System.Windows.Forms.MenuItem menuItemView;
	private System.Windows.Forms.MenuItem menuItemEdit;
	private System.Windows.Forms.MenuItem menuItemFile;
	private System.Windows.Forms.MainMenu mainMenu1;
	public static int documentCount; // static var which keeps track of the document count
								// This is used in the display of the form views

	public static String fileExtension = ".doc";

	/// <summary>
	///   Constructor
	/// </summary>
	public Form1()
	{        
		parentWindow = this;
		//
		// Required for Win Form Designer support
		//
		InitializeComponent();	
		    documentCount=0;    
			CreateDocument();
	 }

	/// <summary>
	///    Clean up any resources being used
	/// </summary>
	protected override void Dispose( bool disposing )
	{
		if( disposing )
		{
			if (components != null) 
			{
				components.Dispose();
			}
		}
		base.Dispose( disposing );
	}

	/// <summary>
	///    Required method for Designer support - do not modify
	///    the contents of this method with the code editor
	/// </summary>
	private void InitializeComponent()
	{
		this.components = new System.ComponentModel.Container();
		System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));
		this.openButton = new System.Windows.Forms.ToolBarButton();
		this.printDoc = new System.Drawing.Printing.PrintDocument();
		this.menuItemOpen = new System.Windows.Forms.MenuItem();
		this.menuItemClose = new System.Windows.Forms.MenuItem();
		this.menuItemView = new System.Windows.Forms.MenuItem();
		this.menuItemToolbar = new System.Windows.Forms.MenuItem();
		this.menuItemStatusbar = new System.Windows.Forms.MenuItem();
		this.saveButton = new System.Windows.Forms.ToolBarButton();
		this.menuItemHelp = new System.Windows.Forms.MenuItem();
		this.menuItemAbout = new System.Windows.Forms.MenuItem();
		this.menuItemNew = new System.Windows.Forms.MenuItem();
		this.menuItemFile = new System.Windows.Forms.MenuItem();
		this.menuItemSave = new System.Windows.Forms.MenuItem();
		this.menuItemSaveAs = new System.Windows.Forms.MenuItem();
		this.menuItem12 = new System.Windows.Forms.MenuItem();
		this.menuItemPrint = new System.Windows.Forms.MenuItem();
		this.menuItemPreview = new System.Windows.Forms.MenuItem();
		this.menuItemExit = new System.Windows.Forms.MenuItem();
		this.menuItem16 = new System.Windows.Forms.MenuItem();
		this.menuItemEdit = new System.Windows.Forms.MenuItem();
		this.menuItemClearAll = new System.Windows.Forms.MenuItem();
		this.printButton = new System.Windows.Forms.ToolBarButton();
		this.toolBar1 = new System.Windows.Forms.ToolBar();
		this.newButton = new System.Windows.Forms.ToolBarButton();
		this.previewButton = new System.Windows.Forms.ToolBarButton();
		this.imageList1 = new System.Windows.Forms.ImageList(this.components);
		this.mainMenu1 = new System.Windows.Forms.MainMenu();
		this.menuItemWindow = new System.Windows.Forms.MenuItem();
		this.menuItemNewWindow = new System.Windows.Forms.MenuItem();
		this.menuItemCascade = new System.Windows.Forms.MenuItem();
		this.menuItemTile = new System.Windows.Forms.MenuItem();
		this.statusBar1 = new System.Windows.Forms.StatusBar();
		this.SuspendLayout();
		// 
		// openButton
		// 
		this.openButton.ImageIndex = 1;
		this.openButton.ToolTipText = "Open";
		// 
		// printDoc
		// 
		this.printDoc.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(this.MDIPrintPage);
		// 
		// menuItemOpen
		// 
		this.menuItemOpen.Index = 1;
		this.menuItemOpen.Text = "Open...";
		this.menuItemOpen.Click += new System.EventHandler(this.MenuItemHandler);
		// 
		// menuItemClose
		// 
		this.menuItemClose.Index = 2;
		this.menuItemClose.Text = "Close";
		this.menuItemClose.Click += new System.EventHandler(this.MenuItemHandler);
		// 
		// menuItemView
		// 
		this.menuItemView.Index = 2;
		this.menuItemView.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																					 this.menuItemToolbar,
																					 this.menuItemStatusbar});
		this.menuItemView.Text = "View";
		// 
		// menuItemToolbar
		// 
		this.menuItemToolbar.Checked = true;
		this.menuItemToolbar.Index = 0;
		this.menuItemToolbar.Text = "Toolbar";
		this.menuItemToolbar.Click += new System.EventHandler(this.MenuItemHandler);
		// 
		// menuItemStatusbar
		// 
		this.menuItemStatusbar.Checked = true;
		this.menuItemStatusbar.Index = 1;
		this.menuItemStatusbar.Text = "Status Bar";
		this.menuItemStatusbar.Click += new System.EventHandler(this.MenuItemHandler);
		// 
		// saveButton
		// 
		this.saveButton.ImageIndex = 2;
		this.saveButton.ToolTipText = "Save";
		// 
		// menuItemHelp
		// 
		this.menuItemHelp.Index = 4;
		this.menuItemHelp.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																					 this.menuItemAbout});
		this.menuItemHelp.Text = "Help";
		// 
		// menuItemAbout
		// 
		this.menuItemAbout.Index = 0;
		this.menuItemAbout.Text = "About MDI Application";
		this.menuItemAbout.Click += new System.EventHandler(this.MenuItemHandler);
		// 
		// menuItemNew
		// 
		this.menuItemNew.Index = 0;
		this.menuItemNew.Text = "New";
		this.menuItemNew.Click += new System.EventHandler(this.MenuItemHandler);
		// 
		// menuItemFile
		// 
		this.menuItemFile.Index = 0;
		this.menuItemFile.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																					 this.menuItemNew,
																					 this.menuItemOpen,
																					 this.menuItemClose,
																					 this.menuItemSave,
																					 this.menuItemSaveAs,
																					 this.menuItem12,
																					 this.menuItemPrint,
																					 this.menuItemPreview,
																					 this.menuItemExit});
		this.menuItemFile.Text = "File";
		// 
		// menuItemSave
		// 
		this.menuItemSave.Index = 3;
		this.menuItemSave.Text = "Save";
		this.menuItemSave.Click += new System.EventHandler(this.MenuItemHandler);
		// 
		// menuItemSaveAs
		// 
		this.menuItemSaveAs.Index = 4;
		this.menuItemSaveAs.Text = "Save As...";
		this.menuItemSaveAs.Click += new System.EventHandler(this.MenuItemHandler);
		// 
		// menuItem12
		// 
		this.menuItem12.Index = 5;
		this.menuItem12.Text = "-";
		// 
		// menuItemPrint
		// 
		this.menuItemPrint.Index = 6;
		this.menuItemPrint.Text = "Print...";
		this.menuItemPrint.Click += new System.EventHandler(this.MenuItemHandler);
		// 
		// menuItemPreview
		// 
		this.menuItemPreview.Index = 7;
		this.menuItemPreview.Text = "Print Preview";
		this.menuItemPreview.Click += new System.EventHandler(this.MenuItemHandler);
		// 
		// menuItemExit
		// 
		this.menuItemExit.Index = 8;
		this.menuItemExit.Text = "Exit";
		this.menuItemExit.Click += new System.EventHandler(this.MenuItemHandler);
		// 
		// menuItem16
		// 
		this.menuItem16.Index = -1;
		this.menuItem16.Text = "-";
		// 
		// menuItemEdit
		// 
		this.menuItemEdit.Index = 1;
		this.menuItemEdit.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																					 this.menuItemClearAll});
		this.menuItemEdit.Text = "Edit";
		// 
		// menuItemClearAll
		// 
		this.menuItemClearAll.Index = 0;
		this.menuItemClearAll.Text = "Clear All";
		this.menuItemClearAll.Click += new System.EventHandler(this.MenuItemHandler);
		// 
		// printButton
		// 
		this.printButton.ImageIndex = 4;
		this.printButton.ToolTipText = "Print";
		// 
		// toolBar1
		// 
		this.toolBar1.Buttons.AddRange(new System.Windows.Forms.ToolBarButton[] {
																					this.newButton,
																					this.openButton,
																					this.saveButton,
																					this.previewButton,
																					this.printButton});
		this.toolBar1.DropDownArrows = true;
		this.toolBar1.ImageList = this.imageList1;
		this.toolBar1.Name = "toolBar1";
		this.toolBar1.ShowToolTips = true;
		this.toolBar1.Size = new System.Drawing.Size(588, 25);
		this.toolBar1.TabIndex = 1;
		this.toolBar1.ButtonClick += new System.Windows.Forms.ToolBarButtonClickEventHandler(this.toolBar1_ButtonClick);
		// 
		// newButton
		// 
		this.newButton.ImageIndex = 0;
		this.newButton.ToolTipText = "New";
		// 
		// previewButton
		// 
		this.previewButton.ImageIndex = 3;
		this.previewButton.ToolTipText = "Print Preview";
		// 
		// imageList1
		// 
		this.imageList1.ColorDepth = System.Windows.Forms.ColorDepth.Depth8Bit;
		this.imageList1.ImageSize = new System.Drawing.Size(16, 16);
		this.imageList1.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("imageList1.ImageStream")));
		this.imageList1.TransparentColor = System.Drawing.Color.Transparent;
		// 
		// mainMenu1
		// 
		this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																				  this.menuItemFile,
																				  this.menuItemEdit,
																				  this.menuItemView,
																				  this.menuItemWindow,
																				  this.menuItemHelp});
		// 
		// menuItemWindow
		// 
		this.menuItemWindow.Index = 3;
		this.menuItemWindow.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																					   this.menuItemNewWindow,
																					   this.menuItemCascade,
																					   this.menuItemTile});
		this.menuItemWindow.Text = "Window";
		// 
		// menuItemNewWindow
		// 
		this.menuItemNewWindow.Index = 0;
		this.menuItemNewWindow.Text = "New Window";
		this.menuItemNewWindow.Click += new System.EventHandler(this.MenuItemHandler);
		// 
		// menuItemCascade
		// 
		this.menuItemCascade.Index = 1;
		this.menuItemCascade.Text = "Cascade";
		this.menuItemCascade.Click += new System.EventHandler(this.MenuItemHandler);
		// 
		// menuItemTile
		// 
		this.menuItemTile.Index = 2;
		this.menuItemTile.Text = "Tile";
		this.menuItemTile.Click += new System.EventHandler(this.MenuItemHandler);
		// 
		// statusBar1
		// 
		this.statusBar1.Font = new System.Drawing.Font("Arial", 8F);
		this.statusBar1.Location = new System.Drawing.Point(0, 384);
		this.statusBar1.Name = "statusBar1";
		this.statusBar1.Size = new System.Drawing.Size(588, 16);
		this.statusBar1.TabIndex = 2;
		this.statusBar1.Text = "For Help,  press F1";
		// 
		// Form1
		// 
		this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
		this.ClientSize = new System.Drawing.Size(588, 400);
		this.Controls.AddRange(new System.Windows.Forms.Control[] {
																	  this.toolBar1,
																	  this.statusBar1});
		this.IsMdiContainer = true;
		this.Menu = this.mainMenu1;
		this.Name = "Form1";
		this.Text = "MDI sample Application";
		this.Closing += new System.ComponentModel.CancelEventHandler(this.ClosingMainAppHandler);
		this.ResumeLayout(false);

	}

	//Handle the Menu Item clicks
	private void MenuItemHandler(object sender, System.EventArgs e)
	{
		if(sender == menuItemNew)	
		{
			New();
		}
		else if(sender == menuItemOpen)
		{
			Open();
		}
		else if(sender == menuItemSave)
		{
			Save();
		}
		else if(sender == menuItemPreview)
		{
			PrintPreview();
		}
		else if(sender == menuItemPrint)
		{
			Print();
		}
		else if(sender == menuItemAbout)
		{
			AboutHelp();
		}
		else if(sender == menuItemExit)
		{
			Exit();
		}
		else if(sender == menuItemClose)
		{
			CloseView();
		}
		else if(sender == menuItemTile)
		{
			Tile();
		}
		else if(sender == menuItemCascade)
		{
			Cascade();
		}
		else if(sender == menuItemClearAll)
		{
			Clear();
		}
		else if(sender == menuItemSaveAs)
		{
			Save();
		}
		else if(sender == menuItemNewWindow)
		{
			NewWindow();
		}
		else if(sender == menuItemToolbar)
		{
			toolBar1.Visible  = menuItemToolbar.Checked  = !toolBar1.Visible ;
		}
		else if(sender == menuItemStatusbar)
		{
			statusBar1.Visible = menuItemStatusbar.Checked = !statusBar1.Visible;			
		}
	}

	//Handle the Toolbar button clicks
	 private void toolBar1_ButtonClick(object sender, System.Windows.Forms.ToolBarButtonClickEventArgs e)
	{
		if(e.Button == newButton)	
		{
			New();
		}
		else if(e.Button==openButton)
		{
			Open();
		}
		else if(e.Button==saveButton)
		{
			Save();
		}
		else if(e.Button == previewButton)
		{
			PrintPreview();
		}
		else if(e.Button == printButton)
		{
			Print();
		}
	}

	//About Help
	private void AboutHelp()
	{
		MessageBox.Show("MDI App Version 1.0\nBuilt From MS Scribble sample", "About MDIApp");
	}


	//Print
	private void Print()
	{
		try 
		{			
			printDoc.Print();
		} 
		catch(Exception e)
		{
			MessageBox.Show(e.ToString());
		}
        
	}

	//PrintPage event handler
	private void MDIPrintPage(object sender,PrintPageEventArgs ev)
	{
		try{
			MDIView activeView = (MDIView)this.ActiveMdiChild;
			MDIDoc activeDoc = activeView.GetDocument();		

			// TODO
			// add graphic commands here, for instance ev.Graphics....



			ev.HasMorePages = false;
		}

		catch (Exception ex)
		{
			MessageBox.Show(ex.ToString());
		}
	}


	//PrintPreview
	private void PrintPreview()
	{
		try
		{		
			PrintPreviewDialog prevDlg = new PrintPreviewDialog();
			prevDlg.Document = printDoc;
			prevDlg.Size = new System.Drawing.Size(600, 329);
			prevDlg.ShowDialog();
		}
		catch(Exception ex)
		{
			MessageBox.Show(ex.ToString());
		}
		
	}

	//Exit
	private void Exit()
	{
		Form[] childForm = this.MdiChildren ;
		//Make sure to ask for saving the doc before exiting the app
		for(int i=0; i < childForm.Length ; i++)
			childForm[i].Close();

		Application.Exit();
		
	}

	//Close the View
	private  void CloseView()
	{
		MDIView activeView = (MDIView)this.ActiveMdiChild;
		activeView.Close();		
	}

	//Tile
	private void Tile()
	{
		this.LayoutMdi(MdiLayout.TileHorizontal);
		
	}

	//Cascade
	private void Cascade()
	{
		this.LayoutMdi(MdiLayout.Cascade);
		
	}

	//Clear the contents of the active document
	private void Clear()
	{
		MDIView activeView = (MDIView)this.ActiveMdiChild;
		if(activeView != null)
		{
			MDIDoc activeDoc = activeView.GetDocument();
			activeDoc.DeleteContents();
		}
	}
	
	//Open an existing document
	private void Open()
	{
		OpenFileDialog openDlg = new OpenFileDialog();
		openDlg.Filter  = "MDI Files (*" + fileExtension + ")|*" + fileExtension + "|All Files (*.*)|*.*";
		openDlg.FileName = "" ;
		openDlg.DefaultExt = fileExtension;
		openDlg.CheckFileExists = true;
		openDlg.CheckPathExists = true;
		
		DialogResult res = openDlg.ShowDialog ();
		
		if(res == DialogResult.OK)
		{
			if( !(openDlg.FileName).EndsWith(fileExtension) && 
				!(openDlg.FileName).EndsWith(fileExtension.ToUpper())) 
				MessageBox.Show("Unexpected file format","MDI App",MessageBoxButtons.OK );
			else
			{
				if(this.ActiveMdiChild == null)			
					EnableItems();		
			
				MDIDoc newDoc = CreateDocument();
				newDoc.OpenDocument(openDlg.FileName);
			}
		}		
		
	}

	//Save the document
	private void Save()
	{
		MDIView selectedView = (MDIView)this.ActiveMdiChild ;
		SaveFileDialog saveDlg = new SaveFileDialog();
		saveDlg.Filter = "MDI Files (*"+ fileExtension +")|*"+ fileExtension + "|All Files (*.*)|*.*";
		saveDlg.DefaultExt = fileExtension;
		DialogResult res = saveDlg.ShowDialog ();
		
		if(res == DialogResult.OK)
			selectedView.GetDocument().SaveDocument(saveDlg.FileName);		

	}

	//Open new document
	private void New()
	{		
		//If this is the first child window, enable the Menu and Toolbar items
		if(this.ActiveMdiChild == null)
			EnableItems();
		CreateDocument();						
	}
    
	//NewWindow
	private void NewWindow()
	{
		MDIView activeView = (MDIView)this.ActiveMdiChild;
		MDIView newView = new MDIView(activeView.GetDocument(),parentWindow);
		newView.GetDocument().viewList.Add(newView);
		newView.Show();	
	}

	//Creates a new document
	private MDIDoc CreateDocument()
	{
		MDIDoc newDoc = new MDIDoc (parentWindow);
		documentCount++;
		return newDoc;
	}


	//Disable the menu and toolbar items when there is no active child form
	public void DisableItems()
	{
		this.menuItemEdit.Visible=false;
		this.menuItemWindow.Visible=false;
		this.menuItemClose.Visible=false;
		this.menuItemSave.Visible=false;
		this.menuItemSaveAs.Visible=false;
		this.menuItemPrint.Visible=false;
		this.menuItemPreview.Visible=false;
		this.saveButton.Enabled= false;
		this.previewButton.Enabled=false;
		this.printButton.Enabled=false;
					
	}

	//Enable the menu and toolbar items when the first child form is created
	public void EnableItems()
	{
		this.menuItemEdit.Visible=true;
		this.menuItemWindow.Visible=true;
		this.menuItemClose.Visible=true;
		this.menuItemSave.Visible=true;
		this.menuItemSaveAs.Visible=true;
		this.menuItemPrint.Visible=true;
		this.menuItemPreview.Visible=true;
		this.saveButton.Enabled= true;
		this.previewButton.Enabled=true;
		this.printButton.Enabled=true;
					
	}

	//App closing handler
	public void ClosingMainAppHandler(Object sender,CancelEventArgs e)
	{
		this.Exit();		
	}

	/*
	* The main entry point for the application.
    *
    */
	[STAThread]
	public static void Main(string[] args) 
	{
	Application.Run(new Form1());
	}
}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.


Written By
France France
Addicted to reverse engineering. At work, I am developing business intelligence software in a team of smart people (independent software vendor).

Need a fast Excel generation component? Try xlsgen.

Comments and Discussions