Solved

A simpler way of accomplishing the task

Posted on 1998-09-02
22
509 Views
Last Modified: 2008-02-20
In my previous questions, I was attempting to create a new version of the usual windows encapsulated save as/save dialog box. Now I understand there is a much simpler way to accomplish what I want to do.
My goal is to have an address book dialog based program (already made that) with 6 variables in it (have that too) and to allow the user once they have chosen the save as/open option from the menu-to be able to save a file with a specific file name and then retrieve it. This file name should pop up prechosen in a serperate dialog box that will pop up when they chose the save/open option from the menu. When they hit enter, the process will take place. Now.. my question is, how to accomplish this? I realize I need to create that custom dialog box, link it to my main box, and serialize those variables for storage and retrieval. I have that part already done. Basically what I need to know is how do I attach that serialize function to take place once the user hits the enter button on the custom dialog box? DO I attach this code to that enter button? Or to the save/load option on the menu of my main dialog box? Do I use the following command to call that custom box?
dlg.m_FileSaveLoad = m_FileSaveLoad;
   dlg.DoModal();

if so, whats the next step in my code? Also how do I specify that specific file name & path to be present in that custom dialog box?
This is the code I have so far:
___________
UpdateData(TRUE);    // update m_FileSaveLoad
   SSampDlg dlg;
// We have a string, show the dialog.
                dlg.m_FileSaveLoad = m_FileSaveLoad;
                dlg.DoModal();
        }
}(then load or save the archived data?)



_______
Sorry for the repeated questions, but I really want to make this work.
THanks!
0
Comment
Question by:kittles
  • 12
  • 10
22 Comments
 

Author Comment

by:kittles
ID: 1171968
Adjusted points to 100
0
 
LVL 2

Accepted Solution

by:
jrmcg earned 100 total points
ID: 1171969
If you were to use the CFileDialog as was discussed in your previous quesions/answers, the loading and saving would be done after the SaveAs dialog returned a File Name.  I am assuming that you have decided to use your own "SaveAs" custom dialog box.  The serialization code can be executed in either dialog box.  It would be up to you.  I would suggest doing it this way though.....

Assuming that in your main dialog box you have an edit box or something that already contains a file path:

int iResult;
SSampleDlg dlg;

UpdateData(TRUE);

iResult = dlg.DoModal();

now - inside the InitInstance function of the custom dialog,

// IDC_EDIT_CONTROL is the ID of the edit box in your main dlg that has the file path.
// m_CustomFileName is a CString member variable of your edit box inside your custom
// dialog box.

((CEdit*)GetDlgItem(IDC_EDIT_CONTROL))->GetWindowText(m_CustomFileName);

UpdateData(FALSE);

The code for when the OK button is pushed should:

UpdateData(TRUE);

// Then copy the contents of m_CustomFileName to the edit box in your Main Dialog.

((CEdit*)GetDlgItem(IDC_EDIT_CONTROL))->SetWindowText(m_CustomFileName);

// then call the OnOk() function.  This will return a value of IDOK to the iResult integer
// used with DoModal() in your main dialog.

OnOk();

When you return back to your main dialog.....

Previously called....
iResult = dlg.DoModal();

if(iResult == IDOK)
{
   UpdateData(TRUE); // get the value that is in your window
   // Perform your serialization using the new file from the custom dialog.
}
else  // they either closed the dialog or pushed cancel
{
   // Don't do anything.
}

Let me know if you need any further help with this or any explanations..

Best Regards,
J.R.


0
 

Author Comment

by:kittles
ID: 1171970
thanks so much for your patient explaination. I will apply this to my code this evening and let you know if I have any real problems tommorow. Hopefully though, I can pull it off with this great example. Thanks again!
0
 
LVL 2

Expert Comment

by:jrmcg
ID: 1171971
You are very welcome.  Like I said just let me know if you need further help with it.

Good Luck :)
J.R.
0
 

Author Comment

by:kittles
ID: 1171972
Hello again:) Sorry to ask for more help already.. but I do have a question. It's probably a simple one- but here goes. I don't have that file name\path  (C:\TEST.DAT) specified anywhere in my program, although thats precisely the default I want in the edit box of the custom save as/open dialog box each time it opens. How can I specify that to appear in the edit box of the custom dialog box, without having it already in an edit box of the main dialog box? I have 6 variables in the main dialog box, such as name, address, etc. and want those saved to this file under that specified filename/path. Then I want to call to that custom box from the main window when they chose saveas/open from the menu..that will call and bring up the custom dialog box with the predefined path/file in it.
What I'm left with is- how to serialize the variables into the correct file name (I already have them serializing, just not to the right place) and how to call those back using that menu command save/open. I also need to know how to specify C:\TEST.DAT as my default path/filename in the edit box of the custom dialog box window.
Sorry I repeated so much, but it's kind of hard to be clear when it comes to this topic:)
Here is my code as is: before I added the custom dialog box. I still need to know how to call to that custom box from this code:
____________________
void CExercizeDlg::OnFileSave()
{



      //Update m_Var1Edit and all others with the screen contents.
      UpdateData(TRUE);


      CFile f;
      f.Open("C:\\TEST.DAT",
            CFile::modeCreate | CFile::modeWrite );

      //Create an archive object.
      CArchive ar( &f, CArchive::store );
      
      //Serialize m_Var1Edit and all others into the archive.
      ar << m_Var1Edit << m_Var2Edit << m_Var3Edit << m_Var4Edit << m_Var5Edit << m_Var6Edit;

      //Close the archive.
      ar.Close();

      //Close the file.
      f.Close();
}

void CExercize4Dlg::OnFileLoad()
{

      // Open the file C:\TEST.DAT.

      CFile f;
      if ( f.Open("C:\\Try.TRY", CFile::modeRead)==FALSE )
            return;

      //Create an archive object.
      CArchive ar( &f, CArchive::load );

      //Serialize data from the archive into m_Var1Edit and others.
      ar >> m_Var1Edit >> m_Var2Edit >> m_Var3Edit >> m_Var4Edit >> m_Var5Edit >> m_Var6Edit;

      //Close the archive
      ar.Close();

      //Close the file.
      f.Close();

      //Update screen with the new values of m_Var1Edit and others.
      UpdateData(FALSE);
}

_____________
As you can see I was previously saving the file without the saveas/open commands from the menu, and then recalling them with the load feature from the menu. Now I need to update this code to do as above. I have the custom dialog box ready, and it's named SaveLoadDlg. The edit box (variable) in it is named m_Edit7. Can you give me some ideas? I'm sorry it's so long and detailed and all- but I'm really lost on this one :) Your code above would have worked like a charm, if only I had the filename and path specified in an edit box on the main dialog window.
0
 
LVL 2

Expert Comment

by:jrmcg
ID: 1171973
No problem...

Before I start...
Create a public variable of type CString inside your Application class.
An easy way of doing this is to click on the 'Class' tab of the workspace window.
Right click on your Application class name, not the Dlg class but the App class.
Click Add member variable.
Variable Type = CString
Variable Declaration = name of variable = m_MainFileName ( for instance ).
Make sure public is selected and click OK.

Now, in the Applications .h file probably named ProjectNameApp.h (whatever your project name is).  At the VERY bottom put:

extern CProjectNameApp theApp; // Once again ProjectName is your project name.

Inside the Application's InitInstance put:
m_MainFileName = "C:\\Test.dat";

** what this has done is created a public variable inside your application's main class that can be accessed by both your Main dialog and the Custom dialog.

First of all, since you do not have an edit box in your main dialog that does not contain the default path -- disregard the lines that look like this:

((CEdit*)GetDlgItem(IDC_EDIT_CONTROL))->GetWindowText(m_CustomFileName);
((CEdit*)GetDlgItem(IDC_EDIT_CONTROL))->SetWindowText(m_CustomFileName);

inside the InitInstance of your custom dialog and just set the member variable like this:

m_CustomFileName = "C:\\Test.dat";

then:

UpdateData(FALSE);

The following is your code:
I will implement the way described in my answer.

void CExercizeDlg::OnFileSave()
{
  int iResult; // ADDED
  CSaveLoadDlg dlg; // ADDED

  //Update m_Var1Edit and all others with the screen contents.
  UpdateData(TRUE);
 
  iResult = dlg.DoModal(); // ADDED
 
  if(iResult != IDOK) return;  // ADDED

  CFile f;
   f.Open(theApp.m_MainFileName, // CHANGED THIS
   CFile::modeCreate | CFile::modeWrite );

   //Create an archive object.
   CArchive ar( &f, CArchive::store );

   //Serialize m_Var1Edit and all others into the archive.
   ar << m_Var1Edit << m_Var2Edit << m_Var3Edit << m_Var4Edit << m_Var5Edit <<    m_Var6Edit;

   //Close the archive.
   ar.Close();

   //Close the file.
   f.Close();
}

Your Load function will be done the same way.

Inside your Custom Dialog.
When the OK button is pushed, instead of the ((CEdit*)GetDlgItem.....) line, do this:

UpdateData(TRUE);
theApp.m_MainFileName = m_CustomFileName;
OnOK();

This should do the trick.
I have a question though.  Why did you decide to create your own custom dialog instead of using the CFileDialog that was suggested?  Just wondering.

If you need help with the Application class and the Dialog class that I have referred to just let me know.

J.R.


0
 

Author Comment

by:kittles
ID: 1171974
Thanks so much for your help! That did indeed answer all my questions. Ahh how grateful one can be after messing with something like this for weeks! lol As for why I didn't use the CFileDialog, I wanted to be able to get rid of the explorer style window in the save as box, and simply have the edit box where the user types in the file name and not give them the option to browse the hard drive or directories. I am learning in school how to use custom dialog boxes and although I know I could have found an easier way to implement what I am trying to learn, I "thought" at first this project would be easy for me:) lol Anyway, learning how to use my school work, along with your code help, will force me to learn more than I would if I just kept following the recommended study projects, and to start out a little on my own. I'm hoping to do this alot more often, since following the book's recommended projects can keep you too focused on those at hand, and not in future development projects... atleast thats how I see it. I guess I'll just have to be careful in the future of jumping in over my head, huh? :) thats a promise. Thanks again!
0
 
LVL 2

Expert Comment

by:jrmcg
ID: 1171975
I'm glad that I could help you with what you needed.  Good luck in your future with programming.  You have definitely chosen a great path to head down.  Don't worry about jumping in over your head there's always going to be somebody to help.  I don't think I've ever gotten my head above water.  This profession changes constantly and there is always something new to learn.

Good luck.

J.R.
0
 

Author Comment

by:kittles
ID: 1171976
Thanks! I do have one more little bitty question... I sure hope you arent losing your patience yet :) In the line:
 CFile f;
         f.Open(theApp.m_MainFileName, // CHANGED THIS
         CFile::modeCreate | CFile::modeWrite );

Im getting this error:
Compiling...
Exercize4Dlg.cpp
D:\Msdev\Projects\Exercize4\ExercizeDlg.cpp(202) : error C2065: 'm_MainFileName' : undeclared identifier
D:\Msdev\Projects\Exercize4\ExercizeDlg.cpp(225) : error C2065: 'dlg' : undeclared identifier
Error executing cl.exe.
Exercize4.exe - 2 error(s), 0 warning(s)

This is what my code looks like;
CFile f;
         f.Open(CExercizeDlg.m_MainFileName,
         CFile::modeCreate | CFile::modeWrite );

I just named it mainfilename, is that okay? OR did I make another major fumble? loI Then I followed the steps above as you said, and I think I know where I might have erred. My custom dialog box doesn't have an initinstance function, only the initdialog function, so I cant place that code you gave me for that section. What did I do wrong? :P
0
 
LVL 2

Expert Comment

by:jrmcg
ID: 1171977
Okay - here goes.

Assuming you used the AppWizard to create a dialog application:

You named the project Exercize4.

The wizard created three classes for you and generated files to accompany these classes.  The three should look like this:

Classes -
1.  CExercize4App
2.  CExercize4Dlg
3.  CAboutDlg

the files generated were:
1.  Exercize4.cpp & Exercize4.h
2.  Exercize4Dlg.cpp & Exercize4Dlg.h

3.  Stdafx.cpp & Stdafx.h  (these are put into all wizard generated apps)
4.  Exercize4.rc & Resource.h

the last 2 files (#4) keep your resource information for menus, dialogs, stringtables, etc.

The Files I mentioned in #1 are your MAIN application files,
the .cpp file handles the start of your program.  Inside that file is an InitInstance function which in turn calls your CExercize4Dlg DoModal() function.  This loads your dialog which you would consider your main application.  These 2 files create your 'Application' class.  Anywhere in my previous code that I said to put the changes in the 'Application', I was referring to these files.

Here is the code I am referring to:

Create a public variable of type CString inside your Application class.
An easy way of doing this is to click on the 'Class' tab of the workspace window.
Right click on your Application class name, not the Dlg class but the App class.
** this would be CExercize4App **
Click Add member variable.
Variable Type = CString
Variable Declaration = name of variable = m_MainFileName ( for instance ).
Make sure public is selected and click OK.

Now, in the Exercize4 .h file -  At the VERY bottom put:

       extern CExercize4App theApp;

       Inside the Application's InitInstance put:
       m_MainFileName = "C:\\Test.dat";

** what this has done is created a public variable inside your application's main class that can be accessed by both your Main dialog and the Custom dialog.

You can e-mail me at j.rmcg@mindspring.com or keep sending messages here, either one is fine.

Hope this kind of clears it up...
J.R.
0
 

Author Comment

by:kittles
ID: 1171978
Hello once again, I hope I'm not a total bother! I understand what you
mean now with the applications header and the initinstance.  It is this
part of your answer that I am trying to understand with my code:
_________________
inside the InitInstance of your custom dialog just set the member
variable like this:
m_CustomFileName = "C:\\Test.dat";
then:
UpdateData(FALSE);
____________________
I don't seem to have an initinstance for my custom dialog box, only in
my main application CExercize4Dlg. Is that the one you meant?

Also, did you want me to replace the "CustomFileName" part above with
the name of my custom dialog box name? or with the edit box that I want
the predefined path C:\Test.dat sent to? That edit box is on my custom
dialog box, and it's called edit1.

Basically, I'm mainly confused about what you want me to replace the
MainFileName and CustomFileName with- or do you actually want me to
leave them as they are? I created the public variable as you suggested,
and named it MainFileName just for the heck of it, so I would know what
it was next time I saw it. Is that acceptable? Does it matter what I
call it?

I sure hope these questions arent completely ignorant. I'm still
learning how to read instruction manuals, lol. It's hard when your not
too learned on the information yet :)
I think if I can just declare everything right and get that filename
thing cleared up I'll be cooking right along. I'm really sorry for being
a pest! Right now the only errors I'm getting are the ones I showed you.
I sure do appreciate all your help.
Thanks again for replying!
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 2

Expert Comment

by:jrmcg
ID: 1171979
I sent you an E-mail to kittles@hotmail.com  was that correct?  I received your e-mail that I think said the same thing as above.  Did you get my e-mail? Let me know....

J.R.
0
 

Author Comment

by:kittles
ID: 1171980
The address is kittle@geocities.com
(instead of hotmail) Sorry for the trouble but would really appreciate a repeat send. After this last question is cleared up, I should be set. *knocks on wood several times* :)
Thanks!

P.S. Sorry for the mixup!
0
 
LVL 2

Expert Comment

by:jrmcg
ID: 1171981
I'm sorry about the mixup, I don't know why I thought the other was your address.

You are correct about the CustomDlg class.  It should be OnInitDialog not InitInstance, my mistake, sorry.  OnInitDialog can be added to the Custom Dialog box through the class wizard.  The variable names can be anything you want them to be.

m_CustomFileName is a member variable of type CString that you need to attach to your Edit1 control on the custom dialog.  To assign this:
Right click on the Edit1 control on your custom dialog box
Select CLASS WIZARD,
Select MEMBER VARIABLES tab,
with the Edit1 control ID highlighted click on Add Variable.
for variable name the m_ is there for you, just add the CustomFileName to the end of it.  Make sure variable TYPE is CString.  Then select OK and OK again.
This attached a CString member variable to the Edit1 control.

The public variable, m_MainFileName should have been created in the Application class, not either one of your Dialog classes.
Putting this:
extern CExercize4App theApp;
at the end of your Exercize4.h file will give you a global variable of type CExercizeApp that can be accessed from any class that includes the Exercize4.h header.  The wizard includes this file for you inside your Dlg.cpp files.  To access the public member variable m_MainFileName from your dialog functions you would type:

inside the Custom Dialog OK or Save or Load button clicked...
UpdateData(TRUE);
theApp.m_MainFileName = m_CustomFileName;

Then in your main dialog's save function:
CFile f;
int result = Dlg.DoModal();
if(result != IDOK) return;
//(else)
f.Open(theApp.m_MainFileName,CFile::modeCreate | CFile::modeWrite);

then serialization....
f.Close();

0
 
LVL 2

Expert Comment

by:jrmcg
ID: 1171982
I hope this clears it up.  Just remember that you have 3 files you are working with not 2.  You are working with CExercize4App, CExercize4Dlg, and CSaveLoadDlg.  Changes will be made in all three.  CExercize4App is your MAIN application, CExercize4Dlg is opened from the MAIN application. CSaveLoadDlg is opened from your CExercize4Dlg.
In essence you are creating a public variable CString that can be accessed and changed by both your main dialog and your custom dialog by putting       theApp.   in front of its variable name.
Don't worry, I don't mind the questions, I want to make sure you understand.

Let me know....    J.R.
0
 

Author Comment

by:kittles
ID: 1171983
Wow- you've already enlightened me alot, thanks! The program is finally working, *YEAH!* the only problem now is something strange with the OK button. When I chose Save or load from the menu, the custom box appears correctly- and with the proper path and file name set- except that when I hit OK, I get an error message with the following details:
_________________________
(this program has performed an illegal operation)
EXERCIZE4 caused a stack fault in module KERNEL32.DLL at 0137:bff9a3c0.
Registers:
EAX=00541fb0 CS=0137 EIP=bff9a3c0 EFLGS=00000256
EBX=0065000c SS=013f ESP=00541fac EBP=00542000
ECX=ffffffff DS=013f ESI=00541fc4 FS=3e07
EDX=0053fffc ES=013f EDI=00650000 GS=0000
Bytes at CS:EIP:
5e 8b e5 5d c2 10 00 64 a1 00 00 00 00 55 8b ec
Stack dump:
815bea30 c00000fd 00000000 00000000 bff9a3c0 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000
____________________________-
Here is the code on my OK button as you instructed, I hope that I have not messed it up somehow: (i have changed the custom dialog's name from SaveLoadDlg to Dialog2)
_________________________________
void Dialog2::OnOK()
{
// TODO: Add extra validation here
UpdateData(TRUE);
      theApp.m_MainFileName = m_edit1;
      OnOK();
_________________________________
Do you see what might be wrong with that one? Perhaps it's something to do with my save/load commands? They look just like this:
_______________________
void CExercize4Dlg::OnFileSave()
     
{       int iResult;
        Dialog2 dlg;
       iResult = dlg.DoModal();
     
CFile f;
      int result = dlg.DoModal();
      if(result != IDOK) return;
      //(else)
      f.Open(theApp.m_MainFileName,
              CFile::modeCreate | CFile::modeWrite);
( serialization follows)
_______________________________________________________________
void CExercize4Dlg::OnFileLoad()
{
       int iResult;
        Dialog2 dlg;
iResult = dlg.DoModal();
       CFile f;
      int result = dlg.DoModal();
      if(result != IDOK) return;
      //(else)
      f.Open(theApp.m_MainFileName,
              CFile::modeCreate | CFile::modeWrite);

(serialization follows)
}
_________________________________________________
Could something be amiss there? I wish I could tell you how much I appreciate your time :) It was GREAT to see that program actually open for the first time today!!!!! hehe
If there is a way for me to award you more points, please let me know as you certainly deserve them!
Laura
0
 
LVL 2

Expert Comment

by:jrmcg
ID: 1171984
Hi Laura,
The only problem I see is that you have opened the dialog twice in both the save and load functions.

The first time you call it:

int iResult;
iResult=dlg.DoModal();

is okay.
But then you open it again with:

int result=dlg.DoModal();

this is not needed in either function.

then your if statement, if(result!=IDOK) return;

should use the first variable    iResult.

if(iResult!=IDOK) return;

Should look like this:
void CExercize4Dlg::OnFileSave()          
{
   int iResult;
   Dialog2 dlg;
   CFile f;

   iResult = dlg.DoModal();            
   if(iResult != IDOK) return;
     
   f.Open(theApp.m_MainFileName,
   CFile::modeCreate | CFile::modeWrite);
   ( serialization follows)
}    _______________________________________________________________

void CExercize4Dlg::OnFileLoad()
{
   int iResult;
   Dialog2 dlg;
   CFile f;

   iResult = dlg.DoModal();
   if(iResult != IDOK) return;

   f.Open(theApp.m_MainFileName,
  CFile::modeCreate | CFile::modeWrite);

  (serialization follows)
}

If taking these out and changing the result to iResult in the if statement doesn't work, send me the code where you declare m_MainFileName and the name of the file that it is in.

0
 
LVL 2

Expert Comment

by:jrmcg
ID: 1171985
Don't worry about the points,  I just like to help.  If this is all still confusing you may call me directly at 1-800-241-2308.  I am on the East coast at work from 8:00am to 5:00pm EST.
If you want to call, just ask for J.R. and they will direct your call.

Let me know.

J.R.
0
 

Author Comment

by:kittles
ID: 1171986
Thanks for your response! I have made the changes above to my code, but am still recieving that stack overflow error. I have copied what I think you wanted below. It is from Exercize4.h where I placed the m_mainfilename line:

__________________________

// Exercize4.h : main header file for the EXERCIZE4 application
//

#ifndef __AFXWIN_H__
      #error include 'stdafx.h' before including this file for PCH
#endif

#include "resource.h"            // main symbols

/////////////////////////////////////////////////////////////////////////////
// CExercize4App:
// See Exercize4.cpp for the implementation of this class
//

class CExercize4App : public CWinApp
{
public:
      CString m_MainFileName;
      CExercize4App();

// Overrides
      // ClassWizard generated virtual function overrides
      //{{AFX_VIRTUAL(CExercize4App)
      public:
      virtual BOOL InitInstance();
      //}}AFX_VIRTUAL

// Implementation

      //{{AFX_MSG(CExercize4App)
            // NOTE - the ClassWizard will add and remove member functions here.
            //    DO NOT EDIT what you see in these blocks of generated code !
      //}}AFX_MSG
      DECLARE_MESSAGE_MAP()
};
extern CExercize4App theApp;

/////////////////////////////////////////////////////////////////////////////

___________________________________-
Hopefully thats the one you wanted? I wish I could see whats messing the program up, but I've read through all my books over and over again and I just can't see what I've done wrong. Here is the newly altered code we went over above:
_______________________________________
void CExercize4Dlg::OnFileSave()
     
{         int iResult;
         Dialog2 dlg;
         CFile f;

         iResult = dlg.DoModal();              
         if(iResult != IDOK) return;
             
         f.Open(theApp.m_MainFileName,
         CFile::modeCreate | CFile::modeWrite);  

(serialization and archive)

      //Close the archive.
      ar.Close();

      //Close the file.
      f.Close();
}

void CExercize4Dlg::OnFileLoad()
{
         int iResult;
         Dialog2 dlg;
         CFile f;

         iResult = dlg.DoModal();
         if(iResult != IDOK) return;

         f.Open(theApp.m_MainFileName,
        CFile::modeCreate | CFile::modeWrite);

      //Create an archive object.
      CArchive ar( &f, CArchive::load );

      //Serialize data .
      ar >> m_Var1Edit >> m_Var2Edit >> m_Var3Edit >> m_Var4Edit >> m_Var5Edit >> m_Var6Edit;

      //Close the archive
      ar.Close();

      //Close the file.
      f.Close();

      //Update screen with the new values of m_Var1Edit and others.
      UpdateData(FALSE);
}
__________________________________________________
Could it maybe be something with my serialization? I have a sneaky suspicion that it's something to do with my OK button or it's commands, although I have not changed that from the one I copied above to you. The program seems to do fine until that button is pressed.
Thank you for the work number- I will use that for the next question- hopefully though I won't have to bother you so much in the future! :) Thanks again,
Laura

P.S. This is the error I'm getting when I try debugging it
_________________________
Unhandled exception in Exercize4.exe (Kernel 32dll):0xC00000FD:stack overflow
_________________________
Then it points me to a line that looks like this:

bff9a3c0   pop       esi

0
 
LVL 2

Expert Comment

by:jrmcg
ID: 1171987
I think I see what it is now.  First of all, (this isn't it) your custom dialog is called Dialog2.  Your class name is Dialog2 and I am guessing that your files are called Dialog2.cpp etc... Just to keep with the proper naming conventions ( for future use ) you should start the name of a new class with a capital 'C'.  Your class name should read CDialog2.  Just kind of a guideline to follow.  It is okay the way it is though.

Alright.  The problem that I see is that your OnOK function is calling itself over and over again.

OnOK() is a CDialog member function which essentially closes the dialog box and returns a value of IDOK to the calling function that opened the dialog with DoModal().  What you have done is Overridden the Member function CDialog::OnOK() with your function Dialog2::OnOK().  When it gets to the OnOK() function inside the button click event it calls itself over and over again at the end.


To illustrate:

1.  User presses OK button.
2.  Dialog2::OnOK() is called.
{
3.  UpdateData(TRUE);
4.  theApp.m_MainFileName = m_edit1;
5.  OnOK();
}

Normally it would assume line 5 was the CDialog::OnOK() and exit the program.  But your function Dialog2::OnOK() is called instead so.......  Do you see the loop?  This is what it means when you hear or read the term "Overriding the function".

To fix this you can do one of two things.

1.  replace line 5 with CDialog::OnOK();
     which ensures the proper function will be called, or
2.  Rename your Dialog2::OnOK() function.

I would use number two, but either one will do.

I would rename your function to:
void Dialog2::OnButtonOK()
{
   // code
}
just make sure you change the declaration of the function inside the .h file. It will be:
void OnButtonOK();

I am pretty sure this is going to solve your problem.

As far as the error you received.....

You will hear two terms repeatedly.
Stack and Heap.

The "Stack" is loaded with variables whose memory is declared and deleted during run-time like iResult inside the function.  The "Heap" variables are declared one time and keep the same memory location throughout the program like m_MainFileName.  Of course this is a "nutshell" rough explaination.

What happened was..... Inside your OnOK() function iResult was being put on the "Stack" over and over again until the "Stack", which is, like memory, not infinite, was "Overflowed".

Make the changes I have suggested and you should have no problem...

Let me know how it works.

J.R.
0
 

Author Comment

by:kittles
ID: 1171988
Wow-& Yeah!!!! It's finally working correctly to save- no more stack overflows- and I understand now how I was looping with the OnOk function. Thanks so much for explaining it to me:)
One more question...I sure sure sure hope this is a little bitty thing- but when I attempt to Load from the menu, I get this little modal dialog box saying "access to-  denied"
(as if the filename is absent??? the space between to and denied) and why is that there anyway? I'm a little confused:)It still opens the dialog2 box and all- I get that wierd error when I hit the ok button. I promise I won't ask anymore questions as soon as this silly thing is working- sorry!! ;)
Here is a copy of my load section:
void CExercize4Dlg::OnFileLoad()
{
         int iResult;
         Dialog2 dlg;
         CFile f;

         iResult = dlg.DoModal();
         if(iResult != IDOK) return;

         f.Open(theApp.m_MainFileName,
        CFile::modeCreate | CFile::modeWrite);

      //Create an archive object.
             CArchive ar( &f, CArchive::load );

      //Serialize data from the archive into m_Var1Edit and others.
      ar >> m_Var1Edit >> m_Var2Edit >> m_Var3Edit >> m_Var4Edit >> m_Var5Edit >> m_Var6Edit;

      //Close the archive
      ar.Close();

      //Close the file.
      f.Close();

}
0
 
LVL 2

Expert Comment

by:jrmcg
ID: 1171989
I guess I should have seen this earlier also.....

You are opening the file for modeWrite, but then trying to read from it.

That should tell you enough right there....  modeRead :-).

9 times out of 10 you are going to find that the hardest bugs to find are really the most simple mistakes.  I do it all the time.

Let me know how it's working....

J.R.

0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now