点击发送后如何更新对话框中的编辑控件?

问题描述 投票:0回答:1

我有一个带有 2 个编辑控件的对话框,一个用于用户输入,另一个可读,仅用于显示用户输入和发送按钮。 我试图通过为 edit_control1 设置一个名为 m_message 的变量来在用户点击发送按钮时显示用户输入。但似乎不起作用。我对 C++ 的经验很少,所以不知道如何正确解决这个问题。

这是我的.cpp 文件:

// 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()
{
  
}

这是我的 .h 文件:

// 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;
};


c++ mfc editcontrol
1个回答
0
投票

在您的 CPP 文件中,您声明了第二个

CString
:

CString m_message;

看到了吗?

dlg_AskAI::dlg_AskAI(CWnd* pParent /*=nullptr*/)
    : CDialog(IDD_ASKAI, pParent)
    , m_message(_T(""))
{

}
--> CString m_message; <--
dlg_AskAI::~dlg_AskAI()
{
}

它不应该存在,因为你在头文件中有一个。

© www.soinside.com 2019 - 2024. All rights reserved.