Hi,

How can I show a long value returned from a function to a textbox on a dialog box.

Void CVideoDlg::OnBnClickedFrames()
{
m_AMC.get_FramesDrawn(); // retunrs long
}

I want to display the returned value in textbox as well as a messagebox.

I tried MessageBox(CString(m_AMC.get_FramesDrawn()));

But it didn't work out

anyone can help with that

thx

Dani AI

Generated

A few practical ways to show the long value (frames drawn) in both a MessageBox and an Edit control — building on ’s note about formatting and Unicode, but using simpler MFC-friendly approaches you can apply immediately.

Use SetDlgItemInt when you just need the numeric text in an edit control (no manual formatting). It writes the decimal string for you and has a signed/unsigned flag:

long frames = m_AMC.get_FramesDrawn();
SetDlgItemInt(IDC_FRAMES_EDIT, (UINT)frames, TRUE); // TRUE = treat value as signed
AfxMessageBox(CString(std::to_wstring(frames).c_str())); // show same text in a message box

If you prefer a control variable (DDX) or want more control over the string, convert the number to a CString and push it to the CEdit:

long frames = m_AMC.get_FramesDrawn();
#ifdef UNICODE
  CString text = std::to_wstring(frames).c_str();
#else
  CString text = std::to_string(frames).c_str();
#endif
m_EditFrames.SetWindowText(text);
AfxMessageBox(text);

If you already use DDX_Text, bind a CString member to the edit (via ClassWizard or DoDataExchange), set that member, and call UpdateData(FALSE) to refresh the control:

// header: CString m_strFrames;
// DoDataExchange: DDX_Text(pDX, IDC_FRAMES_EDIT, m_strFrames);
m_strFrames = text;
UpdateData(FALSE);

Notes and cautions: MessageBox/AfxMessageBox expect LPCTSTR — build a CString first (don’t try to pass a numeric directly). SetDlgItemInt handles formatting for you but only supports 32-bit values on Windows (use 64-bit types and explicit conversion if counts can exceed 32-bit). The approaches above are Unicode-safe when you use the std::to_wstring / std::to_string conversion guarded by UNICODE.

Recommended Answers

All 6 Replies

you need to use CString's Format() method. It works exactly like printf().

CString txt;
txt.Format("%d", m_AMC.get_FramesDrawn()));
MessageBox(txt, MB_OK);

Gives the following error: C2664:\void ATL::CStringT<baset type, string traits>::Format(const wchar_t*,...)': cannot convert parameter 1 from 'const char[3] to const wchar_t*

you are compiling for UNICODE, so param1 should be L"%d" to force the conversion of the string to wchar_t*. Or it might be preferable to use _TEXT("%d") so that you can turn off UNICODE if you want to and not have to change that line.

Thanks man!

you need to use CString's Format() method. It works exactly like printf().

CString txt;
txt.Format("%d", m_AMC.get_FramesDrawn()));
MessageBox(txt, MB_OK);

Sorry One last thing..

How can I display the output value in an edit box?

thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.