Passing CDialog results to CFormView ??
-
I need to configure some application variables. I have a CFormView and via menu conmstruct a simple CDialog. However, after DoModal returns TDOK I am unable to retrieve the CComboBox selection - not even the GetCount returns number of items in the CComboBox. I seldom use CDialog but this got me stumped, I am obviously missing an important piece of code. Thanks for reading and your help is as always appreciated. Here is the offending code: void CFD2008View::OnConfigureSetup() { CString strText; CComboBox* pCombo10; int nIndex; int nCount; // verify document if(!m_CFD2008Doc) m_CFD2008Doc = GetDocument(); VERIFY (m_CFD2008Doc); CDialog dlg(IDD_DIALOG1); if (dlg.DoModal()==IDOK){ TRACE("\nvoid CFD2008View::OnConfigureSetup() "); pCombo10 = (CComboBox *) GetDlgItem(IDC_COMBO10); VERIFY(pCombo10); nIndex = pCombo10->GetCurSel(); //always returns 0 nCount = pCombo10->GetCount(); // always returns 0 } }
-
I need to configure some application variables. I have a CFormView and via menu conmstruct a simple CDialog. However, after DoModal returns TDOK I am unable to retrieve the CComboBox selection - not even the GetCount returns number of items in the CComboBox. I seldom use CDialog but this got me stumped, I am obviously missing an important piece of code. Thanks for reading and your help is as always appreciated. Here is the offending code: void CFD2008View::OnConfigureSetup() { CString strText; CComboBox* pCombo10; int nIndex; int nCount; // verify document if(!m_CFD2008Doc) m_CFD2008Doc = GetDocument(); VERIFY (m_CFD2008Doc); CDialog dlg(IDD_DIALOG1); if (dlg.DoModal()==IDOK){ TRACE("\nvoid CFD2008View::OnConfigureSetup() "); pCombo10 = (CComboBox *) GetDlgItem(IDC_COMBO10); VERIFY(pCombo10); nIndex = pCombo10->GetCurSel(); //always returns 0 nCount = pCombo10->GetCount(); // always returns 0 } }
Try using
pCombo10 = (CComboBox \*) dlg.GetDlgItem(IDC\_COMBO10);
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
-
I need to configure some application variables. I have a CFormView and via menu conmstruct a simple CDialog. However, after DoModal returns TDOK I am unable to retrieve the CComboBox selection - not even the GetCount returns number of items in the CComboBox. I seldom use CDialog but this got me stumped, I am obviously missing an important piece of code. Thanks for reading and your help is as always appreciated. Here is the offending code: void CFD2008View::OnConfigureSetup() { CString strText; CComboBox* pCombo10; int nIndex; int nCount; // verify document if(!m_CFD2008Doc) m_CFD2008Doc = GetDocument(); VERIFY (m_CFD2008Doc); CDialog dlg(IDD_DIALOG1); if (dlg.DoModal()==IDOK){ TRACE("\nvoid CFD2008View::OnConfigureSetup() "); pCombo10 = (CComboBox *) GetDlgItem(IDC_COMBO10); VERIFY(pCombo10); nIndex = pCombo10->GetCurSel(); //always returns 0 nCount = pCombo10->GetCount(); // always returns 0 } }
When DoModal returns, the dialog box window (and combo box) have been destroyed, so attempting to access them won't work. Instead, you could pass a pointer to the CFD2008View into the dialog - one way is
CDialog dlg(IDD_DIALOG1);
dlg.m_pView = this;
if (dlg.DoModal()...Of course, you need to define a member variable in the dialog to hold this pointer (m_pView). Then in the view class, add member variables for the information you're trying to get. Now in the dialog's OnOK function, set the view class variable so they contain the desired information. Note that getting the count, or a pointer to the combo box will be useless as it's going to be destroyed. If you need to know the value of a selected combo box items, you'll need to find that value in the dialog's OnOK, and set that value into the view class. Hope that helps.
Karl - WK5M PP-ASEL-IA (N43CS) PGP Key: 0xDB02E193 PGP Key Fingerprint: 8F06 5A2E 2735 892B 821C 871A 0411 94EA DB02 E193
-
When DoModal returns, the dialog box window (and combo box) have been destroyed, so attempting to access them won't work. Instead, you could pass a pointer to the CFD2008View into the dialog - one way is
CDialog dlg(IDD_DIALOG1);
dlg.m_pView = this;
if (dlg.DoModal()...Of course, you need to define a member variable in the dialog to hold this pointer (m_pView). Then in the view class, add member variables for the information you're trying to get. Now in the dialog's OnOK function, set the view class variable so they contain the desired information. Note that getting the count, or a pointer to the combo box will be useless as it's going to be destroyed. If you need to know the value of a selected combo box items, you'll need to find that value in the dialog's OnOK, and set that value into the view class. Hope that helps.
Karl - WK5M PP-ASEL-IA (N43CS) PGP Key: 0xDB02E193 PGP Key Fingerprint: 8F06 5A2E 2735 892B 821C 871A 0411 94EA DB02 E193
Thanks guys. I used both of yours advices and came up with this. X_ConfigureDialog dlg; // derived from CDialog with IDD_DIALOG1 resource; if (dlg.DoModal()==IDOK){ // pass dialog data to current view // or use directly in current view m_strBand = dlg.m_ComboBox10; m_strMode = dlg.m_ComboBox20; } Conclusion 1. Do not use CDialog "directly" as local variable. 2. Derive view class variable from CDialog - I guess I should have known that. 3. Let DDX/DDV do its stuff - when DoModal completes. It still puzzles me why dlg is valid when used as derived class. I guess MFC / C++ class hierary magic. Thanks Vaclav