Help I want my Dialog data in my Document! 
Author Message
 Help I want my Dialog data in my Document!

I have an SDI MFC application that uses the Doc/View architecture.  I want
to be able to allow the user to set a few startup parameters, so I pop up a
dialog over my main form at the beginning of my app to get the info.  I can
use DDX to store the data in the Dialog object.  That's swell until they OK
off of the object and I want those values later in my app!

It seems to me that the Document object would be the logical place to store
this info.  But how do I get a pointer to my Document object?  I know from
the View object I can use GetDocument().  But how do I do this from a pop up
Dialog (and its associated object)?

This *has* to be amazingly simple, I'm just missing it.  (Actually I'm
coming back to MFC after being away for almost 18 months and my brain is
full of rust...)

Any help would be appreciated,
Michael Kelsey



Sun, 12 Aug 2001 03:00:00 GMT  
 Help I want my Dialog data in my Document!
Here's what I did.  If there's a better way, or even a more MFC pure way,
please let me know.

1)  I made a CMyDialog member variable in my Document class.

2)  In my View's OnInitialUpdate I do this:
     // Show the initialization dialog
     GetDocument()->m_CMyDialog.DoModal();

3)  When the Dialog dismisses my member variable lives on!!!
(Bwah-ha-ha-ha!)

4) When I want to access any of that stuff I use (only when I'm in the View
class, where I have that handy GetDocument() function):
 GetDocument()->m_CMyDialog.m_MyDialogTextBox3Data;

Is there a better way???  Or even a more orthodox one?

Thanks,
Michael Kelsey


Quote:
>I have an SDI MFC application that uses the Doc/View architecture.  I want
>to be able to allow the user to set a few startup parameters, so I pop up a
>dialog over my main form at the beginning of my app to get the info.  I can
>use DDX to store the data in the Dialog object.  That's swell until they OK
>off of the object and I want those values later in my app!

>It seems to me that the Document object would be the logical place to store
>this info.  But how do I get a pointer to my Document object?  I know from
>the View object I can use GetDocument().  But how do I do this from a pop
up
>Dialog (and its associated object)?

>This *has* to be amazingly simple, I'm just missing it.  (Actually I'm
>coming back to MFC after being away for almost 18 months and my brain is
>full of rust...)

>Any help would be appreciated,
>Michael Kelsey



Sun, 12 Aug 2001 03:00:00 GMT  
 Help I want my Dialog data in my Document!

Quote:

> Here's what I did.  If there's a better way, or even a more MFC pure way,
> please let me know.

> 1)  I made a CMyDialog member variable in my Document class.

> 2)  In my View's OnInitialUpdate I do this:
>      // Show the initialization dialog
>      GetDocument()->m_CMyDialog.DoModal();

> 3)  When the Dialog dismisses my member variable lives on!!!
> (Bwah-ha-ha-ha!)

> 4) When I want to access any of that stuff I use (only when I'm in the View
> class, where I have that handy GetDocument() function):
>  GetDocument()->m_CMyDialog.m_MyDialogTextBox3Data;

> Is there a better way???  Or even a more orthodox one?

> Thanks,
> Michael Kelsey



> >I have an SDI MFC application that uses the Doc/View architecture.  I want
> >to be able to allow the user to set a few startup parameters, so I pop up a
> >dialog over my main form at the beginning of my app to get the info.  I can
> >use DDX to store the data in the Dialog object.  That's swell until they OK
> >off of the object and I want those values later in my app!

> >It seems to me that the Document object would be the logical place to store
> >this info.  But how do I get a pointer to my Document object?  I know from
> >the View object I can use GetDocument().  But how do I do this from a pop
> up
> >Dialog (and its associated object)?

> >This *has* to be amazingly simple, I'm just missing it.  (Actually I'm
> >coming back to MFC after being away for almost 18 months and my brain is
> >full of rust...)

> >Any help would be appreciated,
> >Michael Kelsey

That's a fine way to keep the dialog member variables accessible.  But what if the user hoses them up and clicks Cancel?  If that can happen then you need to test for IDOK and laboriously copy the dialog variabls into document variables so the Cancel button will perform as expected.


Sun, 12 Aug 2001 03:00:00 GMT  
 Help I want my Dialog data in my Document!
Michael:

There is nothing really wrong with what you are doing (it works, right?), but I
think you could easily make it more OOP correct, and (therefore) more robust to
modification.

One potential problem is what happens if you have several views. Are you going
to open the dialog from every OnInitialUpdate(). Or just one of them? If you are
not careful your dialog will get called more than once, or not at all.  Also, do
you want this dialog to show if the user opens a previously open document? (I
believe OnInitialUpdate() is called every time you hit "New" or "Open"). Your
problem is that the dialog is really setting properties of the document, but you
are calling it from the view.

A natural solution might seem to be to call the dialog from your
CDocument::OnNewDocument(). There is just one problem. In SDI, the first time
OnNewDocument() is called, the main Window is not yet created, so the dialog
will be shown without the main window behind it, which you might not like. There
are ways to deal with this (like initially creating a dummy null document which
does not open the dialog), but perhaps it is easier to stick with your method of
initializing the call to open the dialog from the OnInitialUpdate(s).

Another point is just style really. Although it is superficially appealing to
hold the data in the dialog, the view really has no business knowing the inner
workings of the document, let alone the dialog which belongs to the document.
Although

GetDocument()->m_CMyDialog.m_MyDialogTextBox3Data;

works if you make everything public, it is not good OOP. If you change the
dialog, you may have to change not only the document, but also the view(s). The
party line is to make access functions in both the document and the dialog, but
this can be tedious.

An "intermediate correct" method to address all these problems might be:

(a) make a BOOL variable in the document m_bCheckWithUser which is "normally"
FALSE, but which you set to TRUE in OnNewDocument() (and perhaps in
OnOpenDocument() if you want).

(b) Make a CString variable m_myString (or whatever) in the document.

(c) Write a function in your document

CMyDocument::CheckWithUser()
{
  if(!m_bCheckWithUser)
    return;

  CMyDialog dlg;
  dlg.m_MyDialogTextBox3Data=m_myString;
  if (dlg.DoModal() == IDOK)
  {
    m_myString=m_MyDialog.m_MyDialogTextBox3Data;
  }
  m_bCheckWithUser=FALSE;

Quote:
}

If you call this CheckWithUser() from each OnInitialUpdate(), It will only open
the dialog once, even if there are multiple views. Also, the string can be
accessed using GetDocument()->m_myString, without reference to the dialog. Even
better would be to write an access function GetMyString() in the document; then
you could make m_myString private or protected.

These things can be very tiresome to do at the beginning, but the idea behind
OOP is that if you make yourself do them, it will make life much easier for you
later.

One way of looking at it is that if you do not use the doc-view classes
"correctly" then they can just be a nuisance, because you have to run around
getting pointers in order to access things (as you have discovered). You are
getting the inconvenience, without the benefit.

HTH,

David Wilkinson

=================

Quote:

> Here's what I did.  If there's a better way, or even a more MFC pure way,
> please let me know.

> 1)  I made a CMyDialog member variable in my Document class.

> 2)  In my View's OnInitialUpdate I do this:
>      // Show the initialization dialog
>      GetDocument()->m_CMyDialog.DoModal();

> 3)  When the Dialog dismisses my member variable lives on!!!
> (Bwah-ha-ha-ha!)

> 4) When I want to access any of that stuff I use (only when I'm in the View
> class, where I have that handy GetDocument() function):
>  GetDocument()->m_CMyDialog.m_MyDialogTextBox3Data;

> Is there a better way???  Or even a more orthodox one?

> Thanks,
> Michael Kelsey



> >I have an SDI MFC application that uses the Doc/View architecture.  I want
> >to be able to allow the user to set a few startup parameters, so I pop up a
> >dialog over my main form at the beginning of my app to get the info.  I can
> >use DDX to store the data in the Dialog object.  That's swell until they OK
> >off of the object and I want those values later in my app!

> >It seems to me that the Document object would be the logical place to store
> >this info.  But how do I get a pointer to my Document object?  I know from
> >the View object I can use GetDocument().  But how do I do this from a pop
> up
> >Dialog (and its associated object)?

> >This *has* to be amazingly simple, I'm just missing it.  (Actually I'm
> >coming back to MFC after being away for almost 18 months and my brain is
> >full of rust...)

> >Any help would be appreciated,
> >Michael Kelsey



Mon, 13 Aug 2001 03:00:00 GMT  
 Help I want my Dialog data in my Document!
i dont know what you guys are smoking.

lets say you have an edit control in that dialog for getting the users
name and then it gets stored in a *private* member CString variable. add
a public member function to your drived CDialog class. Example
prototype      CString CMyDialog::GetUserName();.
This way the dialog data is not open to the world and nothing is
dependant on another class.

So you bring that dialog box to life anywhere you want. you can do this.

CString sBuffer;
CMyDialog dlgTemp;
int nResult = dlgTemp.DoModal();

if(nResult == IDOK)
{
    sBuffer = dlgTemp.GetUserName();    //it is that easy

Quote:
}

This seems simplistic but now you can do whatever you want with that
data and you can use it in more than one place.
You could store it in your view or you document or write it to the
registry.

there is nothing wrong with doing the above in your OnInitialUpdate
function in your view class. If you are concerned with this only taking
place once because of OnInitialUpdate being called each time a new
document is created by the user then do this.

static bool bOnce = false;

if(bOnce == false)
{
    //place the code segment above right here
    bOnce = true;

Quote:
}



Mon, 13 Aug 2001 03:00:00 GMT  
 Help I want my Dialog data in my Document!
Michael,

If I understand u right, it is a one time data that u want to collect at the
application startup ( some config stuff )
So wouldn't CWinApp derived object be the logical place to handle this ? In
the InitInstance function call the dialog and based on whether the user
pressed ok or cancel u will want to use the default data or user specified
data.

m_pMainWnd->UpdateWindow();
m_pDlgObject->SetDefaults(); // Some fuction that u write for setting the
default values
m_pDlgObject->DoModal();

Wherever ( which ever class ) u want to access this data just use
AfxGetApp()->GetStartupData(); // Some function which will fetch the
required data;

Regards,
Anupama



Tue, 14 Aug 2001 03:00:00 GMT  
 Help I want my Dialog data in my Document!
Hey, thanks everyone for all your responses.  It looks like I didn't mess it
up too bad (I guess I should get some points off for not making my variables
private.  My mother taught me that it is bad manners to make your privates
public, I should know better.)

This is a tiny app to help our network guys do some logging.  I popped up
the dialog so that they could tell me what servers and processes they wanted
to monitor.  But that wasn't good enough, they wanted an INI file that would
prepopulate the preferences dialog that they could edit.  Even when dealing
with internal customers, user is a four letter word.

Thanks again for your insights,
Michael Kelsey


Quote:
>I have an SDI MFC application that uses the Doc/View architecture.  I want
>to be able to allow the user to set a few startup parameters, so I pop up a
>dialog over my main form at the beginning of my app to get the info.  I can
>use DDX to store the data in the Dialog object.  That's swell until they OK
>off of the object and I want those values later in my app!

>It seems to me that the Document object would be the logical place to store
>this info.  But how do I get a pointer to my Document object?  I know from
>the View object I can use GetDocument().  But how do I do this from a pop
up
>Dialog (and its associated object)?

>This *has* to be amazingly simple, I'm just missing it.  (Actually I'm
>coming back to MFC after being away for almost 18 months and my brain is
>full of rust...)

>Any help would be appreciated,
>Michael Kelsey



Tue, 14 Aug 2001 03:00:00 GMT  
 Help I want my Dialog data in my Document!

Quote:

> Here's what I did.  If there's a better way, or even a more MFC pure way,
> please let me know.

> 1)  I made a CMyDialog member variable in my Document class.

> 2)  In my View's OnInitialUpdate I do this:
>      // Show the initialization dialog
>      GetDocument()->m_CMyDialog.DoModal();

> 3)  When the Dialog dismisses my member variable lives on!!!
> (Bwah-ha-ha-ha!)

> 4) When I want to access any of that stuff I use (only when I'm in the View
> class, where I have that handy GetDocument() function):
>  GetDocument()->m_CMyDialog.m_MyDialogTextBox3Data;

> Is there a better way???  Or even a more orthodox one?

> Thanks,
> Michael Kelsey



> >I have an SDI MFC application that uses the Doc/View architecture.  I want
> >to be able to allow the user to set a few startup parameters, so I pop up a
> >dialog over my main form at the beginning of my app to get the info.  I can
> >use DDX to store the data in the Dialog object.  That's swell until they OK
> >off of the object and I want those values later in my app!

> >It seems to me that the Document object would be the logical place to store
> >this info.  But how do I get a pointer to my Document object?  I know from
> >the View object I can use GetDocument().  But how do I do this from a pop
> up
> >Dialog (and its associated object)?

> >This *has* to be amazingly simple, I'm just missing it.  (Actually I'm
> >coming back to MFC after being away for almost 18 months and my brain is
> >full of rust...)

> >Any help would be appreciated,
> >Michael Kelsey

Hi,

I had the same problem and I think I got a nice way to solve it.
  You are caling the dialog window from the View class, but not with
".DoModal()".
You take the Create funktion. Everey Dilaog has his own class. So You
can overwrite
the "Create" funktion of this class is this way:

MyDlg.h:
--------
...
#include "MyDoc.h"
...

:public // variables
   m_DocPointer;
...

CMyDlg::Create(.... , .... , CMyDoc* DocPointer)
...

MyDlg.cpp:
----------
...
// was generated by the class withard -> You must change it
// in this way, like You wnat it to be

CMyDlg::Create(.... , .... , CMyDoc* DocPointer)
{
  m_DocPointer = DocPointer;

  CDialog::Create(...,...) //first parameters

Quote:
}

MyView:
-------

...
// calling the Dialog
// You have defined a variable: "CMyDlg  m_dialog;" at Your header file

m_dialog.Create(IDD_MyDilaogResource, this, GetDocument());
// the last parameter is the pointer to CMyDoc
...

....
That is all what You have to do. "m_DocPointer" has now the address of
the CMyDoc.

Have a nice day and don't think to much. :-))))



Sat, 25 Aug 2001 03:00:00 GMT  
 
 [ 8 post ] 

 Relevant Pages 

1. Interchange Data Between Dialogs and Document. Help

2. Initializing list box in dialog from document data

3. how to display Document data in dialog box

4. Data exchange between Login Dialog box and Document

5. Getting to Document data from Dialog Box Class

6. Dialog box document class data exchange

7. Importing selected data to an SDI document from an existing document

8. Help wanted - i8051 Keil-C-Compiler data types

9. Help wanted: Data segment layout

10. Help Wanted, in C assignment and Data Structures

11. help: accessing an XML data island inside an HTML document

12. help: accessing an XML data island inside an HTML document

 

 
Powered by phpBB® Forum Software