首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >mfc的DDX_Control示例

mfc的DDX_Control示例
EN

Stack Overflow用户
提问于 2015-04-04 10:09:22
回答 2查看 7.2K关注 0票数 0

我无法得到DDX_Control的工作示例。

创建对话框时,无法为控件对象创建引用。

谷歌也没有这样的例子。

谢谢。

代码语言:javascript
复制
void CEditDialog::DoDataExchange(CDataExchange* pDX)
{
    CDialog::DoDataExchange(pDX);
    DDX_Control(pDX, IDC_COMBO1, m_cmbBox);
    DDX_Text(pDX, IDC_EDIT1, m_Edit);
}

void CMFCApplicationDDEView::OnActionEdit2()
{
    // TODO: Add your command handler code here
    CEditDialog dlg;
    CString str;
    dlg.m_cmbBox.GetLBText(0, str);

    if (dlg.DoModal() == IDOK)
    {
        MessageBox(dlg.cmbItemStr);
    }
}

dlg.m_cmbBox为空。为什么它是空的,我如何在我的观点中引用它?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-04-04 12:13:23

组合框和对话框的代码是正确的,但是m_cmbBox.GetLBText()不能在DoModal()之前和之后使用,因为没有窗口句柄。重写下面的代码,然后访问combo_str而不是访问窗口

代码语言:javascript
复制
BEGIN_MESSAGE_MAP(CEditDialog, CDialog)
    ON_COMMAND(IDOK, OnOK)
    //...
END_MESSAGE_MAP()

BOOL CEditDialog::OnInitDialog()
{
    BOOL res = CDialog::OnInitDialog();
    //Dialog is created, window handles are available, set text here
    return res;
}

void CEditDialog::OnOK()
{
    //get text before dialog's window handles are destroyed
    int sel = m_cmbBox.GetCurSel();
    if (sel >= 0) m_cmbBox.GetLBText(sel, cmbItemStr);
    CDialog::OnOK();    
}
票数 0
EN

Stack Overflow用户

发布于 2015-04-05 07:12:06

@barmak正确地说,在InitDialog()执行之前不能直接访问对话框控件。

但是,可以使用DDX_CBString设置/检索组合框编辑部分的文本,如下所示:

代码语言:javascript
复制
// in .h file
CString m_cmbItemStr;

// in .cpp
void CEditDialog::DoDataExchange(CDataExchange* pDX)
{   CDialog::DoDataExchange(pDX);
    DDX_CBString(pDX, IDC_COMBO1, m_cmbItemStr);
    DDX_Text(pDX, IDC_EDIT1, m_Edit);
}

void CMFCApplicationDDEView::OnActionEdit2()
{   CEditDialog dlg;
    CString str = TEXT("some value");
    dlg.m_cmbItemStr = str;

    if (dlg.DoModal() == IDOK)
        MessageBox(dlg.m_cmbItemStr);
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29445145

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档