我无法得到DDX_Control的工作示例。
创建对话框时,无法为控件对象创建引用。
谷歌也没有这样的例子。
谢谢。
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为空。为什么它是空的,我如何在我的观点中引用它?
发布于 2015-04-04 12:13:23
组合框和对话框的代码是正确的,但是m_cmbBox.GetLBText()不能在DoModal()之前和之后使用,因为没有窗口句柄。重写下面的代码,然后访问combo_str而不是访问窗口
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();
}发布于 2015-04-05 07:12:06
@barmak正确地说,在InitDialog()执行之前不能直接访问对话框控件。
但是,可以使用DDX_CBString设置/检索组合框编辑部分的文本,如下所示:
// 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);
}https://stackoverflow.com/questions/29445145
复制相似问题