An Application of Multiple Forms

 

Using Multiple Forms

When you create a Windows Application, it starting form is made available to you. If one form is not enough for your application, you can add as many as necessary. To add (or to create) a (new) form, you should display the Add New Item dialog box. To do this,

  • on the main menu, you can click File -> Add New Item...

  • on the main menu, you can click Project -> Add New Item...

  • In the Solution Explorer, you can right-click the name of the project and click Add New Item...

On the Add New Item dialog box, in the Templates section, click Window Form (.NET), provide a name in the Name edit box and click Open.

If your application is using various forms and you want to display a particular one at design time, on the main menu, you can click Window. On the list of files,

  • to display the "physical" view of a form, click its name that has [Design]

  • To display the source code of a form, click its name that has the .h

If you visually add two (or more) forms to your application, you may need to link them, allow one to call the other. Since each form is created in its own header file, to let one form know about the existence of another, you must include its header. A form or unit that wants to communicate with another is called a client of the form.

  1. Create a new Windows Application and name it MultiForms1
  2. While the new form is still displaying, in the Properties window, click Text and type
    Central Unit
  3. Click Size, type 520, 350 and press Enter
  4. To add another form, on the main menu, click File -> Add New Item...
  5. In the Add New Item dialog box, in the Templates list, click Windows Form (.NET)
  6. Replace the contents of the Name edit box with Second and press Enter
  7. While the second form is still displaying, in the Properties window, edit its Text property to display Second Step
  8. Set its ShowInTaskbar property to False
  9. To display the first form, in the code editor, click the Form1.h [Design] tab
  10. In the Properties window, click the Events button Events
  11. Double-click the DoubleClick field and implement its event as follows:
     
    #pragma once
    
    #include "Second.h"
    
    namespace MultiForms1
    {
    	using namespace System;
    	using namespace System::ComponentModel;
    	using namespace System::Collections;
    	using namespace System::Windows::Forms;
    	using namespace System::Data;
    	using namespace System::Drawing;
    
    	/// <summary> 
    	/// Summary for Form1
    	///
    	/// WARNING: If you change the name of this class, you will need to change the 
    	///          'Resource File Name' property for the managed resource compiler tool 
    	///          associated with all .resx files this class depends on.  Otherwise,
    	///          the designers will not be able to interact properly with localized
    	///          resources associated with this form.
    	/// </summary>
    	public __gc class Form1 : public System::Windows::Forms::Form
    	{	
    	public:
    		Form1(void)
    		{
    			InitializeComponent();
    		}
      
    	protected:
    		void Dispose(Boolean disposing)
    		{
    			if (disposing && components)
    			{
    				components->Dispose();
    			}
    			__super::Dispose(disposing);
    		}
    
    	private:
    		/// <summary>
    		/// Required designer variable.
    		/// </summary>
    		System::ComponentModel::Container * components;
    
    		/// <summary>
    		/// Required method for Designer support - do not modify
    		/// the contents of this method with the code editor.
    		/// </summary>
    		void InitializeComponent(void)
    		{
    			// 
    			// Form1
    			// 
    			this->AutoScaleBaseSize = System::Drawing::Size(5, 13);
    			this->ClientSize = System::Drawing::Size(512, 323);
    			this->Name = S"Form1";
    			this->Text = S"Central Unit";
    			this->DoubleClick += new System::EventHandler(this, Form1_DoubleClick);
    
    		}	
    	private: System::Void Form1_DoubleClick(System::Object *  sender, System::EventArgs *  e)
    			 {
    				 Second *dos = new Second;
    
    				 dos->Show();
    			 }
    
    	};
    }
  12. Execute the application
  13. If you double-click the first form, the second would display. You may find out that there is a problem in the fact that every time you double-click the first form, a new instance of the second form displays, which may not be very professional
  14. Close it and return to MSVC
  15. Display the second form. Using the Events button in the Properties window, access its Closing event and implement it as follows:
     
    private:
        System::Void Second_Closing(System::Object *  sender,
                                    System::ComponentModel::CancelEventArgs *  e)
        {
    	    // When the user attempts to close the form, don't close it
    	    e->Cancel = true;
    	    // Only hide it
    	    this->Visible = false;
        }
  16. Display the header file of the first form and change it as follows:
     
    #pragma once
    
    #include "Second.h"
    
    namespace MultiForms1
    {
    	. . .
    	public __gc class Form1 : public System::Windows::Forms::Form
    	{	
    	private:
    		Second *dos;
    	public:
    		Form1(void)
    		{
    			InitializeComponent();
    			dos = new Second;
    		}
      
    	. . .
     
    	private: System::Void Form1_DoubleClick(System::Object *  sender,
    	                                        System::EventArgs *  e)
    	{
    		dos->Visible = true;
    	}
    	};
    }
  17. Execute the application
     
    Two Forms
  18. Close it and return to MSVC

The Multiple Document Interface (MDI)

 

Introduction to MDI-Based Applications

A multiple document interface, abbreviated MDI, is an application whose main form directly "owns" other forms. The main form is also considered the parent. The forms that the parent form owns can display only within the rectangular borders of the parent form. The main idea behind an MDI is to allow the user to open different documents and work on them without having to launch another instance of the application.

As opposed to an MDI, a Single Document Interface (SDI) allow only one document at a time in the application.

WordPad is an example of an SDI. The user can open only one document at a time. If he wants another WordPad document, he must open an additional instance of WordPad.

Each form that is child of the main form in an MDI can be a fully functional form and most, if not all, child forms are of the same kind. There are two main ways a child document of an MDI displays. To provide information about its state, a child form is equipped with a title bar. The title bar of a child form displays an icon, the name of its document, and its own system buttons. Since it can be confined only within the parent form, it can be minimized, maximized, or restored within the parent form. When a child form is not maximized, it clearly displays within the parent form. If it gets closed and there is no other child form, the parent form would appear empty. If there are various child forms, they share the size of the client area of the parent form. If one of the child forms is maximized, it occupies the whole client area of the main form and it displays its name on the title bar of the main form.

MDI Creation

It is not particular difficult to create an MDI application. The challenge may come when you need to configure its functionality. To start, you must set a form to be the eventual parent of the application. This can be taken care of by setting its IsMDIContainer property to true. After doing this, the body of the form is painted in gray with a sunken client area that indicates that it can host other form.

After creating the parent form of the MDI, you must provide a way to display a child form when needed. This is usually done with a menu, which we haven't covered yet. When displaying a child form of the MDI, access its MdiParent property and assign it the name of the parent form. Then display the child.

  1. Start a new Windows Application and name it MDITest1
  2. While the main form is still displaying, in the Properties window, set its IsMdiContainer property to True
  3. Click Size, type 620, 400 and press Enter
  4. To add another form that will be used as a child, on the main menu, click Project -> Add New Item...
  5. In the Add New Item dialog box, in the Templates list, click Windows Form (.NET)
  6. Replace the contents of the Name edit box with Baby and press Enter
  7. To display the first form, in the code editor, click the Form1.h [Design] tab
  8. Double-click in the middle of the first form to access its Load() event and implement it as follows:
     
    #pragma once
    
    #include "Baby.h"
    
    namespace MDITest1
    {
    	using namespace System;
    	using namespace System::ComponentModel;
    	using namespace System::Collections;
    	using namespace System::Windows::Forms;
    	using namespace System::Data;
    	using namespace System::Drawing;
    
    	/// <summary> 
    	/// Summary for Form1
    	///
    	/// WARNING: If you change the name of this class, you will need to change the 
    	///          'Resource File Name' property for the managed resource compiler tool 
    	///          associated with all .resx files this class depends on.  Otherwise,
    	///          the designers will not be able to interact properly with localized
    	///          resources associated with this form.
    	/// </summary>
    	public __gc class Form1 : public System::Windows::Forms::Form
    	{	
    	public:
    		Form1(void)
    		{
    			InitializeComponent();
    		}
      
    	protected:
    		void Dispose(Boolean disposing)
    		{
    			if (disposing && components)
    			{
    				components->Dispose();
    			}
    			__super::Dispose(disposing);
    		}
    
    	private:
    		/// <summary>
    		/// Required designer variable.
    		/// </summary>
    		System::ComponentModel::Container * components;
    
    		/// <summary>
    		/// Required method for Designer support - do not modify
    		/// the contents of this method with the code editor.
    		/// </summary>
    		void InitializeComponent(void)
    		{
    			// 
    			// Form1
    			// 
    			this->AutoScaleBaseSize = System::Drawing::Size(5, 13);
    			this->BackColor = System::Drawing::SystemColors::Control;
    			this->ClientSize = System::Drawing::Size(608, 373);
    			this->IsMdiContainer = true;
    			this->Name = S"Form1";
    			this->Text = S"Form1";
    			this->Load += new System::EventHandler(this, Form1_Load);
    
    		}	
    	private: System::Void Form1_Load(System::Object *  sender, System::EventArgs *  e)
    			 {
    				 MDITest1::Baby *MyBaby = new MDITest1::Baby;
    				 MyBaby->MdiParent = this;
    				 MyBaby->Show();
    			 }
    
    	};
    }
  9. Execute the application
     
    A simple MDI application
  10. Close it and return to MSVC
 

Previous Copyright © 2003-2015, FunctionX Next