Change a button's caption in VC++ MFC?

Posted on 2003-03-18
Medium Priority
Last Modified: 2006-11-17
How can I make a program that once a start button is clicked, the same button's caption
changes to stop. I need to work in VC++ MFC.
Question by:TMM
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Expert Comment

ID: 8161692
call the buttons SetWindowText member function.

Expert Comment

ID: 8162007
Can you tell me more aobut the button?

I didn't test it, but I would just try making an OnUpdate_ButtonName((CCmdUI* pCmdUI)) function with the Classwizard.
And than a simple
   pCmdUI->SetText (desired_text_string);

and in OnButton() you have somthing like
static bool  started =! started;  //or global with defined init state :)
if (started) strcpy(desired_text_string,"Stop");
else strcpy(desired_text_string,"Start");
with desired_text_string being a global.
Feel free to use CString instead of *char, if you want.

I don't bet it works (I only used it with menus, neverwith buttons), but it's worth a try if noone has a better solution.


Expert Comment

ID: 8162028
keithas approach seems a little bit less complicated ;-).
(As I only say: Only what's easy is good)
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Expert Comment

ID: 8163278
you can also go to the microsoft msdn to read about button controls


basically, the method suggested by keithas, SetWindowText, should work well.

Accepted Solution

Joeisanerd earned 80 total points
ID: 8163808
Use the following function

SetDlgItemText(hDlg, IDC_BUTTON1, "Stop");

hDlg is the Window Handle to the Dialog box, IDC_BUTTON1 is the id for the button control and "Stop" is the new text. You can also use GetDlgItemText to determine what is the current text on the button to see what action to take.

The details are below.

BOOL SetDlgItemText(
HWND hDlg,
int nIDDlgItem,
LPCTSTR lpString);

Microsoft MSDN article

//Get Dialog Item Text
UINT GetDlgItemText(
HWND hDlg,
int nIDDlgItem,
LPTSTR lpString,
int nMaxCount);

Expert Comment

ID: 8168968
That's the reason I asked for more info on the button, because noone said, that the button is part of a dialog... ;-)

Author Comment

ID: 8169338
Ok. Thanks you too! :)

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question