Click here to Skip to main content
15,887,746 members
Articles / Desktop Programming / MFC

A Beginners Guide to Dialog Based Applications - Part One

Rate me:
Please Sign up or sign in to vote.
4.66/5 (94 votes)
11 Aug 2000 827.8K   196   146
A step by step tutorial showing how to create your first windows program using MFC

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:

Image 1

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.

Image 2

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.

Image 3

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.

Image 4

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

Image 5

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 are 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:

Image 6

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.

Image 7

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.

Image 8

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.

Image 9

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.

Image 10

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.

Image 11

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.

Image 12

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

C++
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.

Image 13

This program could be part of a large database project, but for now we going to leave it 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.

License

This article has no explicit license attached to it, but may contain usage terms in the article text or the download files themselves. If in doubt, please contact the author via the discussion board below. A list of licenses authors might use can be found here.


Written By
Web Developer
United Kingdom United Kingdom
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
AnswerRe: How to write the the data to a .txt file Pin
cristitomi15-Feb-07 5:04
cristitomi15-Feb-07 5:04 
QuestionanCan someone clarify this for me? Pin
Member 195600516-May-05 6:14
Member 195600516-May-05 6:14 
QuestionCan someone clarify this for me? Pin
Member 195600516-May-05 6:00
Member 195600516-May-05 6:00 
AnswerRe: Can someone clarify this for me? Pin
John M. Drescher16-May-05 6:26
John M. Drescher16-May-05 6:26 
QuestionSo, what about buttons if i'm want to delete the data? Pin
tetrrturwfw29-Dec-04 13:35
susstetrrturwfw29-Dec-04 13:35 
GeneralFull Name didnt appear Pin
tennis0413-Oct-04 20:42
tennis0413-Oct-04 20:42 
GeneralRe: Full Name didnt appear Pin
Anonymous1-Dec-04 6:39
Anonymous1-Dec-04 6:39 
GeneralCodeProject gets an 'F' on this one. Pin
7stud7-Oct-04 20:48
7stud7-Oct-04 20:48 
How in the heck can such a short tutorial have so many errors? This is a very simple tutorial, and yet no one at Code Project edited it for mistakes? I think it is of the utmost importance that tutorials for beginners contain NO errors. This is shoddy work and/or editing. Both the author and Code Project should be ashamed. Is this what they are holding up as the standard for tutorials at Code Project. I'm a beginner to Visual C++, but I can rewrite this tutorial and provide screen shots with no mistakes. I rate this tutorial a zero, and I will look elsewhere for other Visual C++ tutorials.

Here are the errors I can remember(there are probably more):

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

When trying to add a variable for IDC_TITLE(the combo box), the only choice for the variable type is int when Category:Value is selected. int is the correct type--not CString. So, if you want to follow the directions, add a variable named m_nTitle with Category: Value and Variable Type: int.

2)
---------
"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…”
---------

There is no "as shown below": no screen shot shows adding a variable to IDC_NAMELIST. To make the program work, you need to add a variable to IDC_NAMELIST called m_NameList and choose Category: Control and Variable Type: CListBox. In addition, you need to do this bit to add ANOTHER variable:

"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."

3)
----------
"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."
---------

The function name is OnAdd()---not Add().

4) There is no explanation of the UpdateData() function. From what little I have read, UpdateData(TRUE) transfers user data from the controls to the variables associated with the controls, and UpdateData(FALSE) transfers data from the variables back to the controls. That is why the function is used. Checking the MSDN library, it says the default parameter for UpdateData is TRUE. Therefore, UpdateData() and UpdateData(TRUE) are equivalent.

5) This line:

nIndex = GetDlgItemText(IDC_TITLE, strTitle);

is introducing us to a function GetDlgItemText(). It apparently gets the data from the control with the ID listed as the first parameter. However, as we found out above, the type of the variable associated with the Value of IDC_TITLE had to be an integer, so the function is going to return an integer. It looks like the integer must be the position of the title in the combo box list. However, we want the actual title--not an integer. Luckily for us, GetDlgItemText() also conveniently puts the title corresponding to the integer position in the variable strTitle.
----------------

6)
-------
" 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."
-------

If you look at the bottom right hand corner of the VC++6 editor window, the dimensions of the dialog box are listed and you can watch the dimensions as you drag to get whatever size you want.

Also, to find out how to easily get controls to line up inside the dialog box, search the MSDN library in the "Visual C++ Documentation" for "layout grid, changing size". Note: Snapping to grid lines only works for controls INSIDE the dialog box--it does not appear to work for the borders of the dialog box itself.

I know the last couple of entries may not be considered mistakes by some people, but I think it was a mistake not to explain those things.

7)Further discussion of mistake 1):

When trying to add a variable for IDC_TITLE(the combo box), the only choice for the variable type is int when Category:Value is selected. int is the correct type--not CString. So, if you want to follow the directions, add a variable named m_nTitle with Category: Value and Variable Type: int.

However, the variable is never used in the program, so as far as I can tell, adding it is unnecessary. As a test, I deleted it from my program, and the program still works.

The thing that I don't understand is: why didn't the author use m_nTitle to retrieve the text from the title combo box. UpdateData() should have transferred the index value of the title into m_nTitle. So, it seems to me we could use that integer to get the text. In order to use that integer to get the text, we would have to find a method, something like GetText() that takes the index position of the title, m_nTitle, as a parameter and returns the text. And, in order to use a method like GetText() we need a variable representing the control so we can call GetText() on it, like this:

myTitleListBox.GetText()

Remembering that we had two choices for the Category of the variables we added: Value or Control, we could go back and add ANOTHER variable to IDC_TITLE and choose Control for the Category in order to get a variable that represents the control itself. I tried it. I added the variable m_CTitle to IDC_TITLE and selected Category: Control and Variable Type: CComboBox (since the only choice for Variable Type is CComboBox, that's a pretty good indication the variable represents the control itself). Then, all I had to do was find a method that would allow me to get the text. I searched around MSDN, but I couldn't find what I was looking for, so I asked in the forums, and I was directed to:

void GetLBText(int nIndex, CString& rString) const;

(To locate the CComboBox methods in MSDN: select Visual C++ Documentation, Contents tab, MSDN Library--April 1999>Visual Studio 6.0 Documentation>Visual C++ Documentation>Reference>Microsoft Foundation Class Library and Templates>Microsoft Foundation Class Library>Class Library Reference>CComboBox>Member Functions. Whew!)

Perfect! The description of the function says it takes the index position as the first parameter, and then fetches the text corresponding to the index position and puts it in the second parameter. To get the text, I then added this to the code:

CString strTitle;

//get currently selected text
m_CTitle.GetLBText(m_nTitle, strTitle);

instead of the author's lines:

CString strTitle ;
int nIndex;

//get currently selected text
nIndex = GetDlgItemText(IDC_TITLE, strTitle);

and the program worked. I think the program makes more sense written that way. Also, I think you have to start exploring how to look things up in MSDN to find any methods you need. There are so many objects and methods, you can't hope to know them all.


GeneralRe: CodeProject gets an 'F' on this one. Pin
timebungary19-Oct-04 21:39
timebungary19-Oct-04 21:39 
GeneralRe: CodeProject gets an 'F' on this one. Pin
ethicalfive17-Feb-05 21:31
ethicalfive17-Feb-05 21:31 
GeneralRe: CodeProject gets an 'F' on this one. Pin
MRAAX5-Dec-05 3:03
MRAAX5-Dec-05 3:03 
GeneralRe: CodeProject gets an 'F' on this one. Pin
ccaprani24-Jan-06 3:13
ccaprani24-Jan-06 3:13 
GeneralRe: CodeProject gets an 'F' on this one. Pin
With_problem29-Aug-06 20:54
With_problem29-Aug-06 20:54 
GeneralMultiline on Cedit wont work Pin
megiddo21-Sep-04 20:28
megiddo21-Sep-04 20:28 
GeneralRe: Multiline on Cedit wont work Pin
tweakhead19-Feb-05 6:00
susstweakhead19-Feb-05 6:00 
GeneralRe: Multiline on Cedit wont work Pin
megiddo20-Feb-05 3:34
megiddo20-Feb-05 3:34 
GeneralHelp me ! Pin
Anonymous18-May-04 21:06
Anonymous18-May-04 21:06 
GeneralRe: Help me ! Pin
Sathiyaraj Ganesan23-Aug-05 22:45
Sathiyaraj Ganesan23-Aug-05 22:45 
Generaland SO Pin
lenl26-Apr-04 5:25
lenl26-Apr-04 5:25 
GeneralDrop List hasn't CString variable type Pin
Member 104755424-Apr-04 14:56
Member 104755424-Apr-04 14:56 
GeneralControl focus Pin
dave@home13-Oct-03 12:11
dave@home13-Oct-03 12:11 
GeneralRe: Control focus Pin
Member 52259713-Oct-03 14:22
Member 52259713-Oct-03 14:22 
GeneralRe: Control focus Pin
Member 52259713-Oct-03 14:23
Member 52259713-Oct-03 14:23 
GeneralRunning Program Pin
Gemged4-Sep-03 0:46
Gemged4-Sep-03 0:46 
GeneralList Scroll Bar Pin
26-Aug-03 6:02
suss26-Aug-03 6:02 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.