Click here to Skip to main content
15,867,686 members
Articles / Desktop Programming / MFC

Using Button Controls in an Application

Rate me:
Please Sign up or sign in to vote.
4.53/5 (89 votes)
22 Aug 2000CPOL 656.7K   8.2K   337   114
How to get a button control wired-in and working

Introduction

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.

Image 1

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.

Image 2

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.

Image 3

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.

Image 4

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.

Image 5

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.

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

History

  • 22nd August, 2000: Initial post

License

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


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

Comments and Discussions

 
GeneralRe: Radio button message processing Pin
zlataneskic22-Mar-07 23:50
zlataneskic22-Mar-07 23:50 
QuestionHow it operate... Pin
Member 6488283-Dec-03 18:59
Member 6488283-Dec-03 18:59 
AnswerRe: How it operate... Pin
Selvam R30-Dec-03 11:37
professionalSelvam R30-Dec-03 11:37 
GeneralAdd button MS Outlook200 and child window Pin
Member 6721434-Nov-03 17:42
Member 6721434-Nov-03 17:42 
GeneralAssistance needed... Pin
crystalyd19-Aug-03 23:30
crystalyd19-Aug-03 23:30 
QuestionHow to Draw a Picture On a CDialog's Form without Any Controls? Pin
ShineStar31-Jul-03 21:20
ShineStar31-Jul-03 21:20 
Generalhelp Pin
sisijian14-Jul-03 8:27
sisijian14-Jul-03 8:27 
GeneralRe: help Pin
Selvam R30-Dec-03 11:16
professionalSelvam R30-Dec-03 11:16 
Hi,

search in MSDN libaray or visit msdn ,

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccore98/HTML/_core_control_topics.asp[^]

with regards,
R.Selvam
QuestionHow to put a bitmap on a button? Pin
borini15-Jun-03 5:31
borini15-Jun-03 5:31 
AnswerRe: How to put a bitmap on a button? Pin
Selvam R30-Dec-03 11:11
professionalSelvam R30-Dec-03 11:11 
QuestionHow to change button caption at run time? Pin
borini28-May-03 22:58
borini28-May-03 22:58 
AnswerRe: How to change button caption at run time? Pin
Abbas_Riazi28-May-03 23:38
professionalAbbas_Riazi28-May-03 23:38 
GeneralRe: How to change button caption at run time? Pin
Atif Farooq29-Aug-04 23:57
Atif Farooq29-Aug-04 23:57 
GeneralRe: How to change button caption at run time? Pin
rabbitzj15-Jun-05 2:31
rabbitzj15-Jun-05 2:31 
AnswerRe: How to change button caption at run time? Pin
Abbas_Riazi28-May-03 23:42
professionalAbbas_Riazi28-May-03 23:42 
QuestionHow to disable a button with bitmap on? Pin
Huba Buba21-May-03 0:26
Huba Buba21-May-03 0:26 
QuestionHow to make a control invisible ? Pin
Dhirendra26-Apr-03 20:35
Dhirendra26-Apr-03 20:35 
AnswerRe: How to make a control invisible ? Pin
fermar8429-Apr-03 5:20
fermar8429-Apr-03 5:20 
GeneralRe: How to make a control invisible ? Pin
Dhirendra29-Apr-03 18:29
Dhirendra29-Apr-03 18:29 
QuestionHow does m_btnCtrl.SetFont work? Pin
Jaltair17-Feb-03 6:07
Jaltair17-Feb-03 6:07 
GeneralCButton in MFC and indows 2000 Pin
newbee27-Jan-03 4:57
newbee27-Jan-03 4:57 
GeneralRe: CButton in MFC and indows 2000 Pin
Anonymous26-May-03 13:41
Anonymous26-May-03 13:41 
Generalconvert Pin
yary19-Dec-02 1:57
yary19-Dec-02 1:57 
GeneralRe: convert Pin
Emcee Lam5-Jan-03 16:46
Emcee Lam5-Jan-03 16:46 
GeneralOpen the Project Pin
ayang_luve21-Nov-02 18:13
ayang_luve21-Nov-02 18:13 

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.