Thread: How to program a "back" button with MFC

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    13

    How to program a "back" button with MFC

    This has been really bothering the crap out of me for the last few days.

    I have dialogue box 1, the person can say continue (goto dialogue box 2) or exit.


    If they continue, then dialogue box 2 let's them exit, or go back to box 1(goto dialgue box 1)

    so it looks something like:

    Code:
    Dialogue Box 1::OnOkay()
     Dialogue box 2   mybox2;
    mybox2.DoModal();
    then for dialogue box 2:

    Code:
    DIalogue box 2::Onback()
    {
    Dialogue box 1 mybox1;
    mybox1.DoModal();
    }
    simple enough - if it clicks okay, it opens a modal dialogue box for 2. if i go back, it opens a modal box for 1. I can use this method and continue forever (i.e., box 1- box 2- box 1- box2). However, if for dialogue box 1 class I declare some arrays, for example, myarray[10001], myarray2[10001], I can only use this method to go back 2 or 3 times before my program crashes on me.

    If I remove these variables, crash goes away (and I'm not using these variables, just declaring them in the public class of dialogue box 1).


    So it would seem that my method doesn't work very well........so my question is, if I go from dialogue box 1, to diaglogue box 2, and in box 2 I want to be able to move back to dialogue box 1, how can I without it crashing (assuming I leave the class variables in box 1)??


    edit: by playing with message handling functions destroy (called when dialogue box destroyed) and init (when dialogue boix is made) i see that my boxes are never being destroyed - i'll create box 1, then i'll create box 2, then i'll create box 1 (without destroying box 2). So I guess that's how I'm running out of memmory.

    Any suggestions on how to do something like:

    create box 1
    user selects to goto box 2
    destroy box 1
    create box 2
    user selects to do back
    destroy box 2
    create box 1



    edit2: Nope, that won't work - I tried calling dialoguebox1/2.DestroyWindow() so it destroys the window when it leaves it for the next dialogue box, but my program STILL crashes on me after a few iterations of moving backwards and forwards


    edit3: Well, it appears a temporary solution is to NOT declare any arrays in the class header if the dialogue box may be created. If I remove all the arrays and just scope them to a function block, as opposed to the entire class, it still doesn't work, so what I have to do is create them on the heap (i.e., float *myarray = new float[10001], then after I destroy the window, call delete [] myarray)

    Humph, i wish there was an easier way around this
    Last edited by 99atlantic; 04-26-2005 at 12:43 AM.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    Well I don't know about how to do things in mfc but modal dialog boxes are intended to be used to wait for the user to close the dialog box before returning from the function which displayed it which is why you are having this problem. If you create 2 modeless dialog boxes you could hide one while the other is being displayed and just switch which one is displayed when you press next/back.

  3. #3
    Chief Code Coloniser!
    Join Date
    Apr 2005
    Posts
    121
    Why not just close dialog box 2 when "back is clicked". Then when the user clicks "forward" again, the dialog box 2 will reopen?

  4. #4
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    I would use modeless dlgs.

    http://cboard.cprogramming.com/showt...light=modeless

    When the user clicks the button check to see if the dlg exists with GetSafeHwnd() if so show the window.

    If not create the dlg.

    When not needed hide the dlg.

    Kill at the apps close.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 11:17 PM
  2. Need help with my program...
    By Noah in forum C Programming
    Replies: 2
    Last Post: 03-11-2006, 07:49 PM
  3. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM
  4. MFC: Multi-threaded DLL Errors & Related Syntax :: C++
    By kuphryn in forum C++ Programming
    Replies: 3
    Last Post: 02-13-2002, 09:02 AM