The Code Project View our advertisersThe intellisense upgrade for Visual C++ - make your IDE as smart as you.Advertise on the CodeProject
Home >> Dialog & Windows >> Beginners

A Beginners Guide to Dialog Based Applications - Part One
By Dr. Asad Altimeemy

A step by step tutorial showing how to create your first windows program using MFC 
  Beginner
  VC 4-6, Win95-98, NT4, W2K, MFC
  Posted 6 Jun 2000
  Updated 12 Aug 2000
Articles by this author
100,308 views
Search:
FAQ
What's New
Lounge
Contribute
Message Boards
Toolbox
Broken links?
Amazon Programming books
Print version
Send to a friend

Sign in / Sign up
 Email
 Password
Remember me?
Lost your Password?
 
Full version Visual Studio .NET from $799
Premium Sponsor
273 users have rated this article. Result:
Popularity: 10.44. Rating: 4.29 out of 5.

Introduction

In this tutorial we are going to create a simple dialog based application. We will use Button, Edit Box, Combo Box, List Box and Static text controls to develop our project. We will be going through the process of adding these controls into their dialog box, using the ClassWizard to link these controls to variables, and handle messages sent by the controls.

I am going to assume you have good knowledge in C++ and you are an absolute beginner to Visual C++. The first thing you need to do is load up your Visual C++ 6 software and create a new project.

Creating a new Project

To create a new project, start by clicking on File in the menu of the Visual C++ 6 window then on New.

The view should look like the image below.

Write Dialog1 in the project name and choose an MFC AppWizard (exe) from the list of the available Wizards, then click OK. The MFC Application Wizard will start.

  • In Step 1 of the Wizard, click on Dialog based option, then click Next.
  • In Steps 2 and 3 accept the default settings, just click Next to do this.
  • In Step 4 click Finish, then you will get the project information. Click OK.

Now the Wizard has created for you a simple empty Dialog Based application. You will need to make few changes and write the necessary code for your application to customise it to your specifications.

Designing the Dialog

Click on the text TODO : Place dialog controls here, then press Delete. This will remove the unwanted text label.

Click on bottom right edge of the dialog box. A rectangle appears around the edges of the box itself. Resize the dialog box using the sizing points as shown below.

Click on the sizing point on the bottom-right corner, keeping the mouse button pressed. Move the mouse, keeping the button pressed. An outline of the new dialog box size is shown as the mouse is moved. Release the mouse button when the dialog box size is 230 x 126.

Click on the Cancel button and press Delete to remove the Cancel button, then right click the mouse on the OK button and a context menu will appear as shown below.

Select Properties from the context menu.

The Push Button Properties dialog box appears. Select the General tab; then in Caption box replace the word OK with Close as shown below.

On the controls toolbar, select the static text control. Click the dialog box near the upper left corner. A static text control appears on the dialog box, with the default text Static.

Static text controls can be used to display some information (we going to use few of them as labels). You need three static text controls. Therefore, you need to repeat the process of dropping static text control on to the dialog box make sure to drop each control next to each other.

On the controls toolbar, select the edit control and as you did before with text control. Drop two edit controls. Select Combo Box from the controls toolbar and drop it into the dialog box. Then select List Box control and drop it to the dialog box. Select button control from the controls toolbar and drop it into the dialog box.

Change the Caption of the new button to Add and its ID to IDC_ADD, then arrange the controls on the dialog box as show below. Right click static text control and change its properties change Caption to Title as shown below.

Change the Caption for each static text control as shown below. Change the ID in Edit Properties for first Edit Box control to IDC_FIRSTNAME and the second edit control to IDC_LASTNAME.

Change the ID in List Box Properties to IDC_NAMELIST and the ID Combo Box Properties to IDC_TITLE.

Click on Data in Combo Box Properties and populate the Data as shown below. After each entry is added in the list remember to press Ctrl + Enter to go to a new line.

On the Styles tab, change the Combo Box's Type to Drop List. Position your mouse over the Combo Box dropdown button then left-click. Another rectangle will appear (see below) that allows you to specify the size of the open Combo Box's List.

Assigning Member variables to Controls

Press Ctrl + W to start ClassWizard, or from the View menu select ClassWizard. The MFC ClassWizard dialog box appears as shown below. Select the Member Variables tab, then select the dialog class in the Class name : combo box; use CDialog1Dlg. Select IDC_FIRSTNAME, click Add Variable button. The dialog box appears (see below). Select Value from Category and select CString from Member variable type.

Type m_strFirstName for Member variable name.

Repeat the same process to add CString member variable m_strLastName for IDC_LASTNAME, and m_strTitle for IDC_TITLE.

For IDC_NAMELIST add a Member variable of Category Control and Variable Type CListBox as shown below. Select the ClassView tab from the project workspace pane. The class list will appear as shown below. Right click CDialog1Dlg class and select add member variable. The Add Member Variable dialog box will appear. Type CString in the variable and m_strFullName in Variable Name.

Adding message handlers for the controls

Press Ctrl + W to start the ClassWizard, or right click the Add button and select ClassWizard from the context menu. Open ClassWizard, select Message Maps tab. In Class name select CDialog1Dlg, on Object IDs select IDC_ADD , then choose BN_CLICKED from the Messages list as shown below.

Click on Add Function. Click OK, then click Edit Code. The ClassWizard will write a new empty function called Add() with the proper prototype in the class header.

Add the code in bold - just below //TODO: Add your control notification handler code here:

void CDialog1Dlg::OnAdd() 
{
	// TODO: Add your control notification handler code here
	
	CString strTitle ;
	int nIndex;

	UpdateData(); 		// Transfer data from controls to variables

	//get currently selected text
	nIndex = GetDlgItemText(IDC_TITLE, strTitle);  //assigning selected 
	m_strFullName = strTitle + " " + m_strFirstName + " " + m_strLastName;

	m_NameList.AddString(m_strFullName);  //Add string to list

	UpdateData(FALSE); 	// Transfer data from variables to controls
}

Building and running the program.

Click on the Title Combo Box and choose a Title. Write your first name in first name edit box and last name in last name edit box then click Add. If everything goes as planned, then you will get your full name and correct title in the list box as shown below.

This program could be part of a large database project, but for now we going to leave at that. I hope you enjoyed the tutorial. In my next tutorial I am going to use some of these controls to develop a simple, but very useful project: a multi-internet search engine and much more.

About Dr. Asad Altimeemy

Asad is the Senior Software Developer dotNet Software UK Ltd www.dotnetsoftware.com. He's been programming in C/C++ for 12 years and Visual C++/MFC,WIN32 for 5 years. BSc, PhD in Physics and Computational Plasma Physics.

Click here to view Dr. Asad Altimeemy's online profile.


Other popular Dialog & Windows articles:

[Top] Sign in to vote for this article:     PoorExcellent  
The Windows Developer Magazine
Premium Sponsor

UCanCode MFC Components with Full Source Code!
  Search (exact phrase)
  View    Per page   Messages since
New thread Msgs 1 to 25 of 72 (Total: 72) (Refresh) First Prev Next Last
Subject  Author  Date 
  Very Good   Brian Delahunty  18:19 11 Sep '02 
  two modal dialogs   Luis Reina  5:30 4 Sep '02 
  Re: two modal dialogs   Luis Reina  7:09 4 Sep '02 
  Style drop list of combo dont enable CString member variable   Gean  16:09 22 Aug '02 
  Control Toolbar Unconfirmed/Anonymous posting  Anonymous  3:41 25 Jul '02 
  who can send me the source?--thank you. Unconfirmed/Anonymous posting  Anonymous  21:21 19 Jul '02 
  Re: who can send me the source?--thank you.   Christian Graus  21:46 19 Jul '02 
  Re: who can send me the source?--thank you.   Brian Delahunty  18:16 11 Sep '02 
  display results in a new window   ajayb  4:46 19 Jul '02 
  Thanks Unconfirmed/Anonymous posting  Anonymous  9:05 20 Jun '02 
  to make visible edit box   MINAKANNAN  1:08 18 Jun '02 
  Dialog App Vs Formview Unconfirmed/Anonymous posting  Anonymous  13:32 23 Apr '02 
  thanks man! u a savior Unconfirmed/Anonymous posting  Rapp  13:38 4 Apr '02 
  Standalone Dialog Based program without mfc dll Unconfirmed/Anonymous posting  La5V  16:29 17 Dec '01 
  Re: Standalone Dialog Based program without mfc dll   Rick York  21:12 17 Dec '01 
  Inserting existing dialog into another dialog Unconfirmed/Anonymous posting  Anonymous  13:51 17 Oct '01 
  Printing Bitmaps from a Dialog Based Application   Gautam  17:10 15 Oct '01 
  Combo problem ??? Unconfirmed/Anonymous posting  Moghrabi  8:30 22 Sep '01 
  Re: Combo problem ??? Unconfirmed/Anonymous posting  JohnyBoy  6:40 30 Jan '02 
  Re: Combo problem ??? Unconfirmed/Anonymous posting  Anand R  7:54 9 May '02 
  Re: Combo problem ??? Unconfirmed/Anonymous posting  Anonymous  4:04 25 Jul '02 
  Internet usage in Dialog bases Unconfirmed/Anonymous posting  Anonymous  8:40 25 May '01 
  Good Article   pendagron  12:44 15 May '01 
  Using edit box for different language Unconfirmed/Anonymous posting  Jayavardhana Rama  6:49 6 May '01 
  Code Corrections Unconfirmed/Anonymous posting  VC++Newbie  12:35 19 Mar '01 
Last Visit: 16:06 Tuesday 24th September, 2002 First Prev Next Last
Home >> Dialog & Windows >> Beginners
Updated: 12 Aug 2000
Editor: Chris Maunder
Article content copyright Dr. Asad Altimeemy, 2000
everything else Copyright © CodeProject, 1999-2002.
Advertise on The Code Project

MSDN CommunitiesDevelopersDexDevGuruProgrammers HeavenTek-Tips ForumsTopXMLVisualBuilder.comW3SchoolsXMLPitstopZVONSearch all Partners