?
Solved

Adding two CString variables

Posted on 2011-05-04
4
Medium Priority
?
1,064 Views
Last Modified: 2012-05-11
Hi,
       I would like to know how to add two CString variables together so that it can be displayed in a MessageBox.  I'm using VS2005, MFC/C++

Example
CString directory = "C:\\Test\\";
CString filename = "Junk.txt";
CString directory_filename = directory + filename;  ??

MessageBox("File Saved to -->" + directory_filename); ??

Thanks,
0
Comment
Question by:arunykand
  • 2
  • 2
4 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 35694055
Basically you got it, yet adding another step might help:
CString directory = "C:\\Test\\";
CString filename = "Junk.txt";
CString directory_filename = directory + filename; 
CString text = "File Saved to --> ";
CString output = text + directory_filename;

MessageBox(NULL,(LPCTSTR)output,"Information",MB_OK);

Open in new window

0
 

Author Comment

by:arunykand
ID: 35694100
JKR....
    I tried your suggestion, I got the following errors:

Error	15	error C2440: 'initializing' : cannot convert from 'const char [9]' to 'ATL::CStringT<BaseType,StringTraits>'	c:\projects\test.cpp	509


Error	18	error C2660: 'CWnd::MessageBoxW' : function does not take 4 arguments	c:\projects\test.cpp	515

Open in new window

0
 
LVL 86

Accepted Solution

by:
jkr earned 2000 total points
ID: 35694185
You are compiling as UNICODE and want to use 'CWnd::MessageBox()'? Then try
CString directory = _T("C:\\Test\\");
CString filename = _T("Junk.txt");
CString directory_filename = directory + filename; 
CString text = _T("File Saved to --> ");
CString output = text + directory_filename;

MessageBox((LPCTSTR)output,_T("Information"),MB_OK);

// alternatively:
::MessageBox(NULL,(LPCTSTR)output,_T("Information"),MB_OK);

Open in new window

0
 

Author Closing Comment

by:arunykand
ID: 35695070
Thanks JKR!
0

Featured Post

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!

Question has a verified solution.

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

There is an easy way, in .NET, to centralize the treatment of all unexpected errors. First of all, instead of launching the application directly in a Form, you need first to write a Sub called Main, in a module. Then, set the Startup Object to th…
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Suggested Courses
Course of the Month13 days, 15 hours left to enroll

807 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