I have a dialog with 2 edit control, one is for user-input, another one is readable only for display whatever user input and send button. I'm trying to display user-input when they hit send button by setting a variable for edit_control1 called m_message. But it seems doesn't work. I have a minimal experience on C++ so not sure how to solve this properly.
this is my .cpp file:
// dlg_AskAI.cpp : implementation file
//
#include "StdAfx.h"
#include "afxdialogex.h"
#include "resource.h"
#include "dlg_AskAI.h"
// dlg_AskAI dialog
IMPLEMENT_DYNAMIC(dlg_AskAI, CDialog)
dlg_AskAI::dlg_AskAI(CWnd* pParent /*=nullptr*/)
: CDialog(IDD_ASKAI, pParent)
, m_message(_T(""))
{
}
CString m_message;
dlg_AskAI::~dlg_AskAI()
{
}
void dlg_AskAI::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
DDX_Text(pDX, IDC_EDIT3, m_message);
}
BEGIN_MESSAGE_MAP(dlg_AskAI, CDialog)
ON_BN_CLICKED(IDC_BUTTON2, &dlg_AskAI::OnBnClickedButton2)
ON_EN_CHANGE(IDC_EDIT3, &dlg_AskAI::OnEnChangeEdit3)
ON_BN_CLICKED(IDC_SEND_BUTTON, &dlg_AskAI::OnBnClickedButton3)
ON_BN_CLICKED(IDC_EDIT4, &dlg_AskAI::OnEnChangeEdit4)
ON_WM_CLOSE()
END_MESSAGE_MAP()
void dlg_AskAI::OnCancel()
{
DestroyWindow();
}
void dlg_AskAI::PostNcDestroy()
{
delete this;
}
//Contact Us
void dlg_AskAI::OnBnClickedButton2()
{
// TODO: Add your control notification handler code here
}
void dlg_AskAI::OnEnChangeEdit3()
{
// TODO: If this is a RICHEDIT control, the control will not
// send this notification unless you override the CDialog::OnInitDialog()
// function and call CRichEditCtrl().SetEventMask()
// with the ENM_CHANGE flag ORed into the mask.
// TODO: Add your control notification handler code here
}
//Send Button
void dlg_AskAI::OnBnClickedButton3()
{
UpdateData(true);
CString formattedMessage;
formattedMessage.Format(_T("Me: %s"), m_message);
// Update the display edit control with the formatted message
SetDlgItemText(IDC_EDIT4, formattedMessage);
GetDlgItem(IDC_EDIT4)->UpdateWindow();
}
void dlg_AskAI::OnEnChangeEdit4()
{
}
this is my .h file :
// dlg_AskAI dialog
class dlg_AskAI : public CDialog
{
DECLARE_DYNAMIC(dlg_AskAI)
public:
dlg_AskAI(CWnd* pParent = NULL); // standard constructor
virtual ~dlg_AskAI();
// Dialog Data
#ifdef AFX_DESIGN_TIME
enum { IDD = IDD_ASKAI };
#endif
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
DECLARE_MESSAGE_MAP();
public:
afx_msg void OnBnClickedButton2();
afx_msg void OnEnChangeEdit3();
afx_msg void OnBnClickedButton3();
afx_msg void OnEnChangeEdit4();
afx_msg void OnCancel();
virtual void PostNcDestroy();
private:
CString m_message;
};
In your CPP file you are declaring a second
CString:See?
That should not be there as you have one in the header file.