FunctionX Practical Learning Logo

Calling a Dialog From a Form

 
 

Introduction

This exercise shows how to call a dialog from a form. To make this example simple, the (simple) dialog box will be called when the user double-clicks the (main) form. Of course, to start, you must possess a form and a dialog box.

  1. Start Microsoft Visual Studio .NET
  2. On the Start Page, click New Project (alternatively, on the main menu, you can click File -> New -> Project...)
  3. On the New Project dialog box, in the Project Types tree list, click Visual C++ Projects
  4. In the Templates list, click Managed C++ Empty Project
  5. In the Name edit box, replace the <Enter name> content with Dialog Caller
  6. In the Location combo box, accept the suggestion or type your own. If you don't have any, type C:\Programs\MSVC.NET
  7. Click OK
  8. On the main menu, click Project -> Add New Item...
  9. In the Add New Item dialog box, in the Templates list, click C++ File
  10. In the Name box, replace <Enter name> with MainForm and click OK
  11. To start with a simple form, the main form, replace the contents of the empty file with the following:
     
    #using <mscorlib.dll>
    #using <System.dll>
    #using <System.Drawing.dll>
    #using <System.Windows.Forms.dll>
    
    using namespace System;
    using namespace System::Drawing;
    using namespace System::Windows::Forms;
    
    __gc class CMainForm : public Form
    {
    public:
    	CMainForm();
    };
    
    CMainForm::CMainForm()
    {
    	// This is the caption of the form
    	this->Text = S"Main Form";
    	// The size of the form Size(Width, Height)
    	this->Size = Drawing::Size(540, 420);
    	// When the form displays, position it to the center of the screen
    	this->StartPosition = FormStartPosition::CenterScreen;
    }
    
    int __stdcall WinMain()
    {
    	CMainForm *MF = new CMainForm();
    	Application::Run(MF);
    
    	return 0;
    }
  12. Press Ctrl + F5 to test the form

Creating a Dialog Box

To create a dialog box, you can derive an object from the Form class, wet the Minimize and the Maximize buttons to false, and set the FormBorderStyle property to FixedDialog.

  1. To create a new class, on the main menu, click Project -> Add Class...
  2. In the Add Class dialog box, in the Categories, make sure Visual C++ is selected. In the Templates list, click Generic C++ Class
  3. Click Open
  4. In the Generic C++ Class Wizard, in the Class Name, type COurDialog
  5. In the Base Class, type Form
  6. Click Finish
  7. To make the form a managed object, change its header file as follows:
     
    #pragma once
    #using <mscorlib.dll>
    #using <System.dll>
    #using <System.Drawing.dll>
    #using <System.Windows.Forms.dll>
    
    using namespace System;
    using namespace System::Drawing;
    using namespace System::Windows::Forms;
    
    __gc class COurDialog : public Form
    {
    public:
    	COurDialog(void);
    	~COurDialog(void);
    };
  8. Save the application

Calling a Dialog Box

  1. To call the other form, create an OnClick event in the main form. In the implementation of the event, call the other form, using a pointer to its class. This is done as follows:
     
    #using <mscorlib.dll>
    #using <System.dll>
    #using <System.Drawing.dll>
    #using <System.Windows.Forms.dll>
    
    #include "OurDialog.h"
    
    using namespace System;
    using namespace System::Drawing;
    using namespace System::Windows::Forms;
    
    __gc class CMainForm : public Form
    {
    public:
    	CMainForm();
    private:
    	// We need the OnDoubleClick event of the form
    	void MainForm_DoubleClick(Object *Sender, EventArgs *Args);
    };
    
    CMainForm::CMainForm()
    {
    	// This is the caption of the form
    	this->Text = S"Main Form";
    	// The size of the form Size(Width, Height)
    	this->Size = Drawing::Size(540, 420);
    	// When the form displays, position it to the center of the screen
    	this->StartPosition = FormStartPosition::CenterScreen;
    
    	// Add the OnDoubleClick event to the events of the form
    	this->add_DoubleClick(new EventHandler(this, MainForm_DoubleClick));
    }
    
    void CMainForm::MainForm_DoubleClick(Object *Sender, EventArgs *Args)
    {
    	COurDialog *Dlg = new COurDialog();
    	Dlg->ShowDialog();
    }
    
    int __stdcall WinMain()
    {
    	CMainForm *MF = new CMainForm();
    	Application::Run(MF);
    
    	return 0;
    }
  2. Test the application and double click in the middle of the main form. After using the forms, close them.
  3. To change the second form into an actual dialog, change its source file as follows:
     
    #include "ourdialog.h"
    #using <mscorlib.dll>
    
    COurDialog::COurDialog(void)
    {
    	// The caption of the dialog box
    	this->Text = "Properties and Options";
    	// Remove the Minimize and the Maximize buttons
    	this->MinimizeBox = false;
    	this->MaximizeBox = false;
    	// Position the dialog box in the center of the form
    	this->StartPosition = FormStartPosition::CenterParent;
    	// Change the border style to FixedDialog value
    	// to make it a dialog box
    	this->FormBorderStyle = FormBorderStyle::FixedDialog;
    }
    
    COurDialog::~COurDialog(void)
    {
    }
  4. Test the application
 

Home Copyright © 2002-2005 FunctionX, Inc.