Click here to Skip to main content
11,935,012 members (52,389 online)
Click here to Skip to main content
Add your own
alternative version


337 bookmarked

Using Button Controls in an Application

, 22 Aug 2000 CPOL
Rate this:
Please Sign up or sign in to vote.
How to get a button control wired-in and working


This tutorial could well be the simplest Windows program you could ever write. All you need to understand this tutorial is the basics of Windows messaging. This tutorial is about the CButton class, and how to get a simple button working.

This tutorial simply takes input for two numbers and depending on what button you click, does the math, i.e., if you enter 1 and 2, then click on 'Plus' the answer will be 3. Pretty basic.

To create this program, first we need to get the framework laid out. Now 99.9% of the time, you will be using buttons via a dialog box so that is where we will start. Open up the AppWizard and create a new project titled ButtonDemo. Just create a basic dialog box without any document / view architecture.

Then click on the ResourceView tab in the 'Workspace' window. Proceed to edit the dialog box IDD_BUTTONDEMO_DLG. It will already contain the Buttons 'Ok' and 'Cancel'. Delete the 'TODO :' message and the 'Cancel' button so we can get to work.

Just drag n' drop the buttons needed, in this example 'Plus' and 'Minus'. Then select and right click to edit their properties. The MFC keeps track of these buttons by their unique ID, a macro located in the "Resource.h" file. For code clarity, change the name of the ID to ID_BUTTON_ADD and modify the caption of the button.

Next, let's add the Edit boxes to house the values to be added or subtracted, do this in a very similar manner to the way you added the buttons. Just drag and drop them in and then modify their ID. We will need an Edit box for the left and right side of the equation as well as one to house the answer. Just for clarity to the end user, check the 'disable' property of the last Edit box. Since we won't directly be able to specify the answer, we will also add a Static control for the equals sign. Just drop one in and then change the caption.

Now that our buttons are in place, we need to wire them into our application. We do this via the class wizard. (Control + W) Click on the Member Variables tab and proceed to add the variables m_nX, m_nY, and m_nAnswer. All integers; these will hold the values inputted from the Edit controls.

Then click on the Message Maps tab. And add a new function for the object ID ID_BUTTON_ADD and ID_BUTTON_SUBTRACT message BN_CLICK. This creates a function which is called whenever that button is clicked once, denoted by OnButtonAdd and OnButtonSubtract.

Now that we have created our buttons, this is all the code we need to write to get this program working. It is fairly self explanatory: what we did was create two functions to be called whenever the respective buttons were pressed. The UpdateData (BOOL) functions are used to manage the data in the Edit boxes of the dialog. UpdataData (TRUE), 'updates' the member variables linked to the Edit boxes to whatever is the current value. UpdataData (FALSE), updates the Edit box to whatever our variable is.

void CButtonDemoDlg::OnButtonAdd() 
	UpdateData (TRUE);
	m_nAnswer = m_nX + m_nY;
	UpdateData (FALSE);

void CButtonDemoDlg::OnButtonSubtract() 
	UpdateData (TRUE);
	m_nAnswer = m_nX - m_nY;
	UpdateData (FALSE);

That is all we need to do to implement a simple button.


  • 22nd August, 2000: Initial post


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Chris Smith
United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralMy vote of 5 Pin
wipin (Vipin Arora)19-Apr-12 19:05
memberwipin (Vipin Arora)19-Apr-12 19:05 
GeneralInstructions Pin
mojens3-Jun-09 11:54
membermojens3-Jun-09 11:54 
Generaltest Pin
Merlin Rose6-May-09 22:23
memberMerlin Rose6-May-09 22:23 
GeneralRe: test Pin
Merlin Rose6-May-09 22:24
memberMerlin Rose6-May-09 22:24 
GeneralMy vote of 1 Pin
Ram Jawahar Pandey12-Jan-09 1:34
memberRam Jawahar Pandey12-Jan-09 1:34 
GeneralRe: My vote of 1 Pin
yathirajkulal29-Dec-10 20:11
memberyathirajkulal29-Dec-10 20:11 
Generalabout the application of the button Pin
MOHYUSUF21-Aug-08 2:23
memberMOHYUSUF21-Aug-08 2:23 
GeneralRe: about the application of the button Pin
woguolyg@126.com11-Nov-08 18:00
memberwoguolyg@126.com11-Nov-08 18:00 
GeneralRe: about the application of the button Pin
yathirajkulal29-Dec-10 20:07
memberyathirajkulal29-Dec-10 20:07 
Generalabout the button application Pin
MOHYUSUF21-Aug-08 2:01
memberMOHYUSUF21-Aug-08 2:01 
GeneralRe: about the button application Pin
yathirajkulal29-Dec-10 19:58
memberyathirajkulal29-Dec-10 19:58 
Generalbutton demo beginner Pin
MOHYUSUF20-Aug-08 13:01
memberMOHYUSUF20-Aug-08 13:01 
GeneralRe: button demo beginner Pin
yathirajkulal29-Dec-10 20:08
memberyathirajkulal29-Dec-10 20:08 
Generaltest [modified] Pin
lfq09062-Jun-08 16:33
memberlfq09062-Jun-08 16:33 
GeneralRe: test Pin
yathirajkulal29-Dec-10 20:13
memberyathirajkulal29-Dec-10 20:13 
GeneralGetting an Error while Adding member variable [modified] Pin
vijay_k24-Apr-08 19:59
membervijay_k24-Apr-08 19:59 
GeneralRe: Getting an Error while Adding member variable Pin
nimeshmca27-Jan-10 19:37
membernimeshmca27-Jan-10 19:37 
GeneralRe: Getting an Error while Adding member variable Pin
nimeshmca27-Jan-10 19:41
membernimeshmca27-Jan-10 19:41 
RantRe: Getting an Error while Adding member variable Pin
nimeshmca27-Jan-10 19:59
membernimeshmca27-Jan-10 19:59 
QuestionConnecting 2 resources using 'Button' control Pin
Samolsharma10-Nov-07 6:42
memberSamolsharma10-Nov-07 6:42 
AnswerRe: Connecting 2 resources using 'Button' control Pin
yathirajkulal29-Dec-10 20:17
memberyathirajkulal29-Dec-10 20:17 
GeneralNeed help Pin
desk11185-Aug-07 22:51
memberdesk11185-Aug-07 22:51 
GeneralChange the color and/or 2 section boxes in 1 button Pin
Davenish10-Apr-07 11:40
memberDavenish10-Apr-07 11:40 
GeneralRe: Change the color and/or 2 section boxes in 1 button Pin
yule7722-Feb-11 15:10
memberyule7722-Feb-11 15:10 
Generalaccessing the tree control Pin
Member #38765198-Mar-07 2:46
memberMember #38765198-Mar-07 2:46 
Questionadd/remove opreation is impossible coz the code element 'CCalcview' is read only Pin
sumit.mishra31-Oct-06 5:47
membersumit.mishra31-Oct-06 5:47 
GeneralButton and focus Pin
ArielR25-Sep-06 6:02
memberArielR25-Sep-06 6:02 
GeneralRe: Button and focus Pin
yathirajkulal29-Dec-10 22:42
memberyathirajkulal29-Dec-10 22:42 
Generalscientific calculator Pin
jalpari30-Apr-06 3:39
memberjalpari30-Apr-06 3:39 
Questioncan we create the default start value for edit box Pin
tekxxxx4-Oct-05 18:57
membertekxxxx4-Oct-05 18:57 
AnswerRe: can we create the default start value for edit box Pin
cristitomi17-Mar-07 5:58
membercristitomi17-Mar-07 5:58 
Generalplease help me to find the mistake of my programe!!!Thank you Pin
linda_zjj29-Aug-05 0:25
memberlinda_zjj29-Aug-05 0:25 
QuestionHow to set the button for wizard application? Pin
death_8320-Jul-05 17:10
memberdeath_8320-Jul-05 17:10 
Generalunderlines and focus rectangle disappear Pin
Ridgeley Yu2-May-05 18:21
memberRidgeley Yu2-May-05 18:21 
GeneralRe: underlines and focus rectangle disappear Pin
Ridgeley Yu2-May-05 18:56
memberRidgeley Yu2-May-05 18:56 
GeneralCreate trial version Pin
rajpalmanish24-Apr-05 3:48
memberrajpalmanish24-Apr-05 3:48 
Generaltransform a bitmap button to other symbol Pin
faridah_hani20-Mar-05 16:10
memberfaridah_hani20-Mar-05 16:10 
GeneralWin32 Programming Pin
radumihai23-Jun-04 23:37
memberradumihai23-Jun-04 23:37 
GeneralChanging default button Pin
Chulips6-May-04 10:30
memberChulips6-May-04 10:30 
GeneralRe: Changing default button Pin
cristitomi18-Mar-07 1:38
membercristitomi18-Mar-07 1:38 
AnswerRe: Changing default button Pin
finalman19-Mar-08 19:36
memberfinalman19-Mar-08 19:36 
GeneralRadio button message processing Pin
Guruhema20-Jan-04 21:51
memberGuruhema20-Jan-04 21:51 
GeneralRe: Radio button message processing Pin
suniljoshi_MCA24-Mar-04 19:10
membersuniljoshi_MCA24-Mar-04 19:10 
GeneralRe: Radio button message processing Pin
zlataneskic23-Mar-07 0:50
memberzlataneskic23-Mar-07 0:50 
QuestionHow it operate... Pin
deven_chin3-Dec-03 19:59
memberdeven_chin3-Dec-03 19:59 
AnswerRe: How it operate... Pin
R.selvam30-Dec-03 12:37
memberR.selvam30-Dec-03 12:37 
GeneralAdd button MS Outlook200 and child window Pin
riaz33bd4-Nov-03 18:42
memberriaz33bd4-Nov-03 18:42 
GeneralAssistance needed... Pin
crystalyd20-Aug-03 0:30
membercrystalyd20-Aug-03 0:30 
QuestionHow to Draw a Picture On a CDialog's Form without Any Controls? Pin
ShineStar31-Jul-03 22:20
memberShineStar31-Jul-03 22:20 
Generalhelp Pin
sisijian14-Jul-03 9:27
membersisijian14-Jul-03 9:27 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.151126.1 | Last Updated 23 Aug 2000
Article Copyright 2000 by Chris Smith
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid