Click here to Skip to main content
13,592,846 members
Click here to Skip to main content
Add your own
alternative version

Tagged as


8 bookmarked
Posted 20 Oct 2010
Licenced CPOL

Formatted MessageBox/AfxMessageBox

, 20 Oct 2010
Rate this:
Please Sign up or sign in to vote.
Need to Format/sprintf a string before displaying a messagebox? Here is solution!
You often need to format a string and populate it with relevant data before displaying a message box. For example
int nAge = 27;
TCHAR sName[]="Ajay";
float nSalary = 12500.50;

// Declare variable
CString strMessage;<br>
// Format
strMessage.Format( _T("Name is %s, Age is %d, and salary is %.2f"), 
    sName, nAge, nSalary);<br>
// Then display.
What if this can be acheived by single statement:
AfxMessageBoxFormatted(_T("Name is %s, Age is %d, and salary is %.2f"), 
   sName, nAge, nSalary);
And here is implementation of AfxMessageBoxFormatted:
void AfxMessageBoxFormatted(LPCTSTR pFormatString, ...)
    va_list vl;
    va_start(vl, pFormatString);<br>
    CString strFormat;
    strFormat.FormatV(pFormatString, vl); // This Line is important!<br>
    // Display message box.	
If you don't you MFC, or don't want to use, you can implement MessageBoxFormatted as:
void MessageBoxFormatted(HWND hWnd, LPCTSTR pCaption, LPCTSTR pFormatString, ...)
    va_list vl;
    va_start(vl, pFormatString);<br>    
    TCHAR strFormat[1024]; // Must ensure size!<br>

    // Generic version of vsprintf, works for both MBCS and Unicode builds 
    _vstprintf(strFormat, pFormatString, vl);<br>	
    // Or use following for more secure code
    // _vstprintf_s(strFormat, sizeof(strFormat), pFormatString, vl)<br>
    ::MessageBox(hWnd, strFormat, pCaption,MB_ICONINFORMATION);
And use it as:
MessageBoxFormatted(NULL,  // Or a valid HWND
    _T("Name is %s, Age is %d, and salary is %.2f"),
    sName, nAge, nSalary);
If you don't understand stuff like TCHAR, LPCTSTR, _T, you better read this Tip/Trick: What are TCHAR, WCHAR, LPSTR, LPWSTR, LPCTSTR etc?[^]


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


About the Author

Ajay Vijayvargiya
Software Developer (Senior)
India India
Started programming with GwBasic back in 1996 (Those lovely days!). Found the hidden talent!

Touched COBOL and Quick Basic for a while.

Finally learned C and C++ entirely on my own, and fell in love with C++, still in love! Began with Turbo C 2.0/3.0, then to VC6 for 4 years! Finally on VC2008/2010.

I enjoy programming, mostly the system programming, but the UI is always on top of MFC! Quite experienced on other environments and platforms, but I prefer Visual C++. Zeal to learn, and to share!

You may also be interested in...


Comments and Discussions

-- There are no messages in this forum --
Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web02-2016 | 2.8.180618.1 | Last Updated 20 Oct 2010
Article Copyright 2010 by Ajay Vijayvargiya
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid