Re: Data Transfer Among Dialogs, Views, and Documents

Tech-Archive recommends: Speed Up your PC by fixing your registry

From: Joseph M. Newcomer (newcomer_at_flounder.com)
Date: 05/11/04


Date: Mon, 10 May 2004 21:40:40 -0400

I would put a pointer to the document class in the dialog,. e.g.,

CWhateverDlg dlg;
dlg.document = GetDocument();
dlg.DoModal();

Then in the OnInitDialog, I would extract the values and use control variables (never,
ever UpdateData) to set the values in the controls, and in the OnOK handler I'd extract
the values from the controls and put them back in the document.

e.g., in OnInitDialog, I'd do

CString s;
switch(document->m_status)
     { /* status */
       case DOCUMENT_READY: // case 1. Of course you use #define, right?
             s.LoadString(IDS_READY);
             break;
       case DOCUMENT_NOT_READY:
            s.LoadString(IDS_NOT_READY);
            break;
       case DOCUMENT_DISABLED:
           s.LoadString(IDS_DISABLED);
            break;
       default:
           s.LoadString(IDS_STATUS_UNKNOWN);
           break;
     } /* status */
c_Status.SetWindowText(s);

It sounds like you are trying this before OnInitDialog has been called, which will always
fail. Until OnInitDialog is called, you can assume the controls do not exist. Therefore,
any attempt to set values before DoModal is called and OnInitDialog follows is doomed.

For example, this will ALWAYS fail, and is SUPPOSED to fail:

        CMyDialog dlg;
        dlg.SetMode(GetDocument()->m_status);
        dlg.DoModal();

where dlg.SetMode contains lines like the above. At the point where you call SetMode,
there IS no dialog. There is a bunch of bits in storage that will eventually reference a
real dialog, but there is no dialog! Only when DoModal is called is the actual dialog and
all its controls created, and until OnInitDialog is called, you have no idea if the
controls exist.

There is no reason to use global variables for this.
                                        joe

On 10 May 2004 17:43:11 -0700, wu_ninja_1999@yahoo.com (Bugenhagen) wrote:

>OS: Windows 2000
>IDE: MS VS .NET 2002
>Language: VC++
>
>Hello
>
>I'm a novice MFC developer and I have run across a problem I was
>hoping someone could assist me with.
>
>I'm developing an SDI application with multiple views. In one of the
>views, I have a button that opens a dialog. I would like to have this
>dialog dynamically modify several of the controls based on information
>from the document class. The controls that need to be modified all
>have associated member control variables added to the dialog class. In
>my application, the view class processes information and stores
>numerical values to various document class member variables. When the
>user clicks a certain button, the objective is to have some of the
>dialog controls take on the values of said document class member
>variables. I'm attempting to accomplish this by creating an instance
>of the dialog class inside one of the view class' member functions.
>Then, I use the control member variable of the dialog class to modify
>it.
>
>For instance, in my dialog class I have a static text control that is
>linked to the member control variable 'c_status'. Depending on the
>value of my document class member variable, 'm_status', I would like
>to do different things to the control. If m_status is equal to 1, I
>want to set the text of the control to read 'Ready'. If it's 0, I want
>it to say 'Not Ready'. If it's -1, I want it to say 'Disabled' and
>actually disable the control. My thinking was that I could call the
>SetWindowText() and EnableWindow() member functions of the control
>variable to do this. Now, my source code compiles with no errors but
>when I run it, I get an assertion failure that is linked to the fact
>that the member variable, 'm_hWnd', member of one of the base classes
>of CDialog (I'm not sure which one it is as I write this) is equal to
>NULL. This occurs as the control's member function is trying to
>execute. This is where I'm stuck. I don't know why it's equal to NULL
>or if that's really the problem. Maybe it's supposed to be NULL
>because my dialog class is a derivative of CDialog. I know I've done
>something similar with a view class based from CFormView and it worked
>fine.
>
>My only alternative is to create global variables for storage and
>modifying these controls inside the dialog class' OnInitDialog()
>member function, where the member functions have no problem executing.
>I don't want to go that route because I believe that is the 'sloppy'
>approach and I'm sure there's a 'correct' way to do it.
>
>In summary, I would like to modify the behavior of some dialog
>controls depending on the value of some document class member
>variables. I'm trying to use the dialog controls' member functions to
>modify them but there's a problem accessing one the dialog class' base
>class member variables thereby preventing this from working. I'm
>seeking a solution and advice on how to properly achieve my goal.
>
>Thanks to anyone who may offer help

Joseph M. Newcomer [MVP]
email: newcomer@flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm


Quantcast