How do I access one dialog's data from another dialog? - Programmer
This is a discussion on How do I access one dialog's data from another dialog? - Programmer ; I have an MFC app, and the main dialog is called MFC_WinPCap.
The MFC_WinPCap dialog calls a 2nd dialog, Prompt_For_Interface_Dlg
(named so because it will prompt the user for a network interface).
I need the second dialog to access data ...
-
How do I access one dialog's data from another dialog?
I have an MFC app, and the main dialog is called MFC_WinPCap.
The MFC_WinPCap dialog calls a 2nd dialog, Prompt_For_Interface_Dlg
(named so because it will prompt the user for a network interface).
I need the second dialog to access data in the first dialog.
In Prompt_For_Interface_Dlg::OnInitDialog(), I made the follwing code
to attempt to access the data in the MFC_WinPCap dialog.
CWnd* pMainDlg = GetDlgItem(IDD_MFC_WINPCAP_DIALOG);
ASSERT(pMainDlg);
CMFC_WinPCapDlg dlg = dynamic_cast(pMainDlg);
ASSERT(dlg);
dlg.ChosenInterface=9;
The first Assert fails, and before I put in the Asserts the assignment
of dlg.ChosenInterface=9 failed to affect the actual ChosenInterface
in the MFC_WinPCap dialog.
I apologize if this has been asked before; my searches came up with
nothing useful.
Thanks.
-
Re: How do I access one dialog's data from another dialog?
Dingus wrote:
> I have an MFC app, and the main dialog is called MFC_WinPCap.
>
> The MFC_WinPCap dialog calls a 2nd dialog, Prompt_For_Interface_Dlg
> (named so because it will prompt the user for a network interface).
>
> I need the second dialog to access data in the first dialog.
>
> In Prompt_For_Interface_Dlg::OnInitDialog(), I made the follwing code
> to attempt to access the data in the MFC_WinPCap dialog.
>
> CWnd* pMainDlg = GetDlgItem(IDD_MFC_WINPCAP_DIALOG);
> ASSERT(pMainDlg);
> CMFC_WinPCapDlg dlg = dynamic_cast(pMainDlg);
> ASSERT(dlg);
> dlg.ChosenInterface=9;
>
> The first Assert fails, and before I put in the Asserts the assignment
> of dlg.ChosenInterface=9 failed to affect the actual ChosenInterface
> in the MFC_WinPCap dialog.
>
> I apologize if this has been asked before; my searches came up with
> nothing useful.
>
> Thanks.
You have multiple coding problems in trying to do this, and design
problems in wanting to do it. One way to do what you are asking is
CMFC_WinPCapDlg* pdlg = (CMFC_WinPCapDlg*)AfxGetMainWnd();
Note it returns a pointer - your cast attempts to convert a pointer into
an object - which is invalid.
There are 3 or 4 ways a user can cancel a dialog. Your dialog should
not be changing anything in the main dialog, because then cancel will
fail to cancel the user's changes. Instead, save the result in a member
variable and let the main dialog read and copy that variable if (and
only if) IDOK is returned by DoModal.
--
Scott McPhillips [VC++ MVP]
-
Re: How do I access one dialog's data from another dialog?
"Scott McPhillips [MVP]" wrote in message news:...
> You have multiple coding problems in trying to do this, and design
> problems in wanting to do it. One way to do what you are asking is
>
> CMFC_WinPCapDlg* pdlg = (CMFC_WinPCapDlg*)AfxGetMainWnd();
>
> Note it returns a pointer - your cast attempts to convert a pointer into
> an object - which is invalid.
>
> There are 3 or 4 ways a user can cancel a dialog. Your dialog should
> not be changing anything in the main dialog, because then cancel will
> fail to cancel the user's changes. Instead, save the result in a member
> variable and let the main dialog read and copy that variable if (and
> only if) IDOK is returned by DoModal.
Thank you Scott, you are right. I started this approach with a desire
to fill a ComboBox in the new dialog with data from the 1st dialog,
and I kinda went too far.
Thanks again for your help.