Skip to main content

Posts

Showing posts with the label CString

How to take input from a MFC Edit Control as string, convert it to integer and append text to it

 Take input from a MFC Control as string : CString txtname; GetDlgItemText(IDC_EDIT1, txtname); Here IDC_EDIT1 is the Control Edit Text and txtname is the input at the moment we call. Convert it to integer or int : int iPort = _atoi( txtname.GetString() ); int iPort = _wtoi( txtname.GetString() ); // if you use wide charater formats     Append text to Edit Control :   void CServerDlg::AppendTextToEditCtrl(CEdit& edit, LPCTSTR pszText) {    // get the initial text length    int nLength = edit.GetWindowTextLength();    // put the selection at the end of text    edit.SetSel(nLength, nLength);    // replace the selection    edit.ReplaceSel(pszText); }

How to convert between 'CString' and 'std::string' ?

ISSUE: When you are developing MFC project in most cases it becomes necessary to convert between 'CString' and 'std::string' . SOLUTION: 'CString' to 'std::string': CString sTest ( "Matrix" ); // Convert a TCHAR string to a LPCSTR CT2CA CStringToAscii(sTest); // construct a std::string using the LPCSTR input std::string sResultedString (CStringToAscii); 'std::string' to 'CString': std::string sTest( " Matrix " ); CString ResultedCString(sTest.c_str());