You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

点击发送按钮后无法更新对话框Edit Control的解决咨询

问题描述

我有一个包含两个Edit Control的对话框,一个用于用户输入(IDC_EDIT3),另一个为只读控件(IDC_EDIT4)用于显示用户输入内容,同时配有发送按钮。我尝试为输入型Edit Control绑定变量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;
};
解决方案
  1. 删除全局变量m_message
    在.cpp文件中,构造函数后定义的全局CString m_message;与类的私有成员m_message重名,导致DDX数据绑定无法正确关联到类成员变量,直接删除这一行即可。

  2. 修正编辑控件的消息映射
    在BEGIN_MESSAGE_MAP中,IDC_EDIT4是编辑控件,不应使用按钮的ON_BN_CLICKED消息,需改为编辑控件的内容变更消息ON_EN_CHANGE,修正后的消息映射行应为:

    ON_EN_CHANGE(IDC_EDIT4, &dlg_AskAI::OnEnChangeEdit4)
    
  3. 验证发送按钮逻辑
    发送按钮的处理函数逻辑是正确的:

    • UpdateData(true):将输入控件(IDC_EDIT3)的内容同步到类成员m_message
    • 格式化消息后通过SetDlgItemText设置到只读控件(IDC_EDIT4)
    • UpdateWindow()可以保留,确保控件立即刷新显示

完成以上修改后,重新编译运行,点击发送按钮即可将输入内容正确显示到只读控件中。

内容的提问来源于stack exchange,提问作者Guobin

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.01 11:37:44