Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Modal and Modeless Dialogs / Non-Working child dialog controls ...

Modal and Modeless Dialogs / Non-Working child dialog controls ...

Scheduled Pinned Locked Moved C / C++ / MFC
c++csharpperformancehelpquestion
8 Posts 4 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    digitalmythology
    wrote on last edited by
    #1

    Thanks in advance, fellow coders. I am creating a dialog based app in MSVC++ 6.0. [No I AM NOT switching to .NET, or should I say .NOT working, LoL). Anyway, my dilema is this : I have 1 main dialog, an AboutBox, and 7 child dialogs. My functions / code for my child dialog buttons wil not work. The exact same code will work when tested in the main dialog or the AboutBox. The AboutBox is a default modal dialog generated by MFC using DoModal. I have now idea how DoModal works [code please?], but I am going for modeless, preferably anyway. This is a snippet of code [from my main dialog's .cpp file], used to dispaly one of the modeless dialog boxes :

    void CInvenStoryDlg::OnButtonNewitem()
    {
    // TODO: Add your control notification handler code here
    CInvenStoryDlg *dlg = new CInvenStoryDlg (/*... construction args ... */);
    dlg->Create(IDD_DIALOG_NEWITEM, this);
    dlg->ShowWindow(SW_SHOW);
    }

    As you all can tell, I have one heck of a memory leak going here. I know I should use DetroyWindow() for modeless dialogs, as oppossed to EndDialog() for Modal ones, but where in the world do I actually PUT that code? I assume in the code for the parent dialog, but I want to be sure. This is a snippet of code [from one of my child dialog's .cpp file]. The code is supposed to be activated when a certain button is clicked. It should read some text from an EditBox into a CSTring, and then fwrite it to a .TXT file in binary mode. [There is a reason that it is in binary mode instead of ASCII, but that is not relevant here.] Anyway, here is the function :

    void CDialogNewItem::OnButtonWriteNew()
    {
    #include FILE *stream;
    int b; // will hold the length of the CString m_newitemname
    GetDlgItemText(IDC_EDIT_ITEMNAME, m_newitemname);
    b = m_newitemname.GetLength();
    MessageBox(m_newitemname);
    // Attempt to open file for write in binary mode:
    // File will be NULLified (erased) if it exists,
    // or created if it does not yet exist ...

    if( (stream = fopen("filename.txt","wb")) != NULL )
    {
    // If open was successful, then execute this code
    fwrite(m_newitemname,1,b,stream);
    fclose(stream);
    }

    else
    // if error occurred, then execute this code instead ...
    MessageBox("File could not be opened\n");
    }

    Yes, yes, I know header includes go up top; this was just for experimental purposes. All will be relocated properly upon final completion ... ;) As I stated before, the [guts] of this func

    C D 2 Replies Last reply
    0
    • D digitalmythology

      Thanks in advance, fellow coders. I am creating a dialog based app in MSVC++ 6.0. [No I AM NOT switching to .NET, or should I say .NOT working, LoL). Anyway, my dilema is this : I have 1 main dialog, an AboutBox, and 7 child dialogs. My functions / code for my child dialog buttons wil not work. The exact same code will work when tested in the main dialog or the AboutBox. The AboutBox is a default modal dialog generated by MFC using DoModal. I have now idea how DoModal works [code please?], but I am going for modeless, preferably anyway. This is a snippet of code [from my main dialog's .cpp file], used to dispaly one of the modeless dialog boxes :

      void CInvenStoryDlg::OnButtonNewitem()
      {
      // TODO: Add your control notification handler code here
      CInvenStoryDlg *dlg = new CInvenStoryDlg (/*... construction args ... */);
      dlg->Create(IDD_DIALOG_NEWITEM, this);
      dlg->ShowWindow(SW_SHOW);
      }

      As you all can tell, I have one heck of a memory leak going here. I know I should use DetroyWindow() for modeless dialogs, as oppossed to EndDialog() for Modal ones, but where in the world do I actually PUT that code? I assume in the code for the parent dialog, but I want to be sure. This is a snippet of code [from one of my child dialog's .cpp file]. The code is supposed to be activated when a certain button is clicked. It should read some text from an EditBox into a CSTring, and then fwrite it to a .TXT file in binary mode. [There is a reason that it is in binary mode instead of ASCII, but that is not relevant here.] Anyway, here is the function :

      void CDialogNewItem::OnButtonWriteNew()
      {
      #include FILE *stream;
      int b; // will hold the length of the CString m_newitemname
      GetDlgItemText(IDC_EDIT_ITEMNAME, m_newitemname);
      b = m_newitemname.GetLength();
      MessageBox(m_newitemname);
      // Attempt to open file for write in binary mode:
      // File will be NULLified (erased) if it exists,
      // or created if it does not yet exist ...

      if( (stream = fopen("filename.txt","wb")) != NULL )
      {
      // If open was successful, then execute this code
      fwrite(m_newitemname,1,b,stream);
      fclose(stream);
      }

      else
      // if error occurred, then execute this code instead ...
      MessageBox("File could not be opened\n");
      }

      Yes, yes, I know header includes go up top; this was just for experimental purposes. All will be relocated properly upon final completion ... ;) As I stated before, the [guts] of this func

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      digitalmythology wrote:

      or should I say .NOT working

      Actually, .NET would deal with your memory leaks, eventually.

      digitalmythology wrote:

      CInvenStoryDlg *dlg = new CInvenStoryDlg (/*... construction args ... */);dlg->Create(IDD_DIALOG_NEWITEM, this);dlg->ShowWindow(SW_SHOW);

      A modeless dialog needs to be a member variable. Otherwise, you've thrown a pointer to the wind, and have no way to do anything with it from there on in.

      digitalmythology wrote:

      (stream = fopen("filename.txt","wb")

      You should use C++ to handle files, C is nasty.

      Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog

      D 1 Reply Last reply
      0
      • C Christian Graus

        digitalmythology wrote:

        or should I say .NOT working

        Actually, .NET would deal with your memory leaks, eventually.

        digitalmythology wrote:

        CInvenStoryDlg *dlg = new CInvenStoryDlg (/*... construction args ... */);dlg->Create(IDD_DIALOG_NEWITEM, this);dlg->ShowWindow(SW_SHOW);

        A modeless dialog needs to be a member variable. Otherwise, you've thrown a pointer to the wind, and have no way to do anything with it from there on in.

        digitalmythology wrote:

        (stream = fopen("filename.txt","wb")

        You should use C++ to handle files, C is nasty.

        Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog

        D Offline
        D Offline
        digitalmythology
        wrote on last edited by
        #3

        Reposting my own code is not much of a help. I need code that shows how to do it correctly. 'A modeless dialog needs to be a member variable. Otherwise, you've thrown a pointer to the wind, and have no way to do anything with it from there on in." I've heard of this solution before, but how do I actually DO it? Just saying it works for most people, but , as I" am a tactical person at times, I need to see actual code. What variable type does the dialog box need to be? CString, int, BOOL, or what? Apologies if I sound crabby, but there are millions pf sites out there on the 'ignorant net', and most issues I can resolve with research, trial, and error. However, there occassionally comes an issue to which everybody has an answer, but nobody wants to show how its actually done. What's the big M$DN secret on these subjects? Thanks, again. I really appreciate it.

        -digitalmythology -dm

        L C 2 Replies Last reply
        0
        • D digitalmythology

          Reposting my own code is not much of a help. I need code that shows how to do it correctly. 'A modeless dialog needs to be a member variable. Otherwise, you've thrown a pointer to the wind, and have no way to do anything with it from there on in." I've heard of this solution before, but how do I actually DO it? Just saying it works for most people, but , as I" am a tactical person at times, I need to see actual code. What variable type does the dialog box need to be? CString, int, BOOL, or what? Apologies if I sound crabby, but there are millions pf sites out there on the 'ignorant net', and most issues I can resolve with research, trial, and error. However, there occassionally comes an issue to which everybody has an answer, but nobody wants to show how its actually done. What's the big M$DN secret on these subjects? Thanks, again. I really appreciate it.

          -digitalmythology -dm

          L Offline
          L Offline
          Leah_Garrett
          wrote on last edited by
          #4

          Try using a member variable of type CInvenStoryDlg. In your header CInvenStoryDlg *m_pdlg ; In your constructor(s) initialise it to NULL m_pdlg = NULL; Change creation code to use the member variable:

          void CInvenStoryDlg::OnButtonNewitem()
          {
          m_pdlg = new CInvenStoryDlg (/*... construction args ... */);
          m_pdlg ->Create(IDD_DIALOG_NEWITEM, this);
          m_pdlg->ShowWindow(SW_SHOW);
          }

          In you destructor check if is not NULL otherwise DestroyWindow.

          1 Reply Last reply
          0
          • D digitalmythology

            Thanks in advance, fellow coders. I am creating a dialog based app in MSVC++ 6.0. [No I AM NOT switching to .NET, or should I say .NOT working, LoL). Anyway, my dilema is this : I have 1 main dialog, an AboutBox, and 7 child dialogs. My functions / code for my child dialog buttons wil not work. The exact same code will work when tested in the main dialog or the AboutBox. The AboutBox is a default modal dialog generated by MFC using DoModal. I have now idea how DoModal works [code please?], but I am going for modeless, preferably anyway. This is a snippet of code [from my main dialog's .cpp file], used to dispaly one of the modeless dialog boxes :

            void CInvenStoryDlg::OnButtonNewitem()
            {
            // TODO: Add your control notification handler code here
            CInvenStoryDlg *dlg = new CInvenStoryDlg (/*... construction args ... */);
            dlg->Create(IDD_DIALOG_NEWITEM, this);
            dlg->ShowWindow(SW_SHOW);
            }

            As you all can tell, I have one heck of a memory leak going here. I know I should use DetroyWindow() for modeless dialogs, as oppossed to EndDialog() for Modal ones, but where in the world do I actually PUT that code? I assume in the code for the parent dialog, but I want to be sure. This is a snippet of code [from one of my child dialog's .cpp file]. The code is supposed to be activated when a certain button is clicked. It should read some text from an EditBox into a CSTring, and then fwrite it to a .TXT file in binary mode. [There is a reason that it is in binary mode instead of ASCII, but that is not relevant here.] Anyway, here is the function :

            void CDialogNewItem::OnButtonWriteNew()
            {
            #include FILE *stream;
            int b; // will hold the length of the CString m_newitemname
            GetDlgItemText(IDC_EDIT_ITEMNAME, m_newitemname);
            b = m_newitemname.GetLength();
            MessageBox(m_newitemname);
            // Attempt to open file for write in binary mode:
            // File will be NULLified (erased) if it exists,
            // or created if it does not yet exist ...

            if( (stream = fopen("filename.txt","wb")) != NULL )
            {
            // If open was successful, then execute this code
            fwrite(m_newitemname,1,b,stream);
            fclose(stream);
            }

            else
            // if error occurred, then execute this code instead ...
            MessageBox("File could not be opened\n");
            }

            Yes, yes, I know header includes go up top; this was just for experimental purposes. All will be relocated properly upon final completion ... ;) As I stated before, the [guts] of this func

            D Offline
            D Offline
            David Crow
            wrote on last edited by
            #5

            digitalmythology wrote:

            void CInvenStoryDlg::OnButtonNewitem() { // TODO: Add your control notification handler code here CInvenStoryDlg *dlg = new c (/*... construction args ... */);

            Is this right, in that you are creating another instance of CInvenStoryDlg?

            digitalmythology wrote:

            I know I should use DetroyWindow() for modeless dialogs, as oppossed to EndDialog() for Modal ones, but where in the world do I actually PUT that code?

            At the point in which you are done with the modeless dialog.

            digitalmythology wrote:

            ...but not in a child dialog.

            Which means what exactly?


            "Approved Workmen Are Not Ashamed" - 2 Timothy 2:15

            "Judge not by the eye but by the heart." - Native American Proverb

            D 1 Reply Last reply
            0
            • D David Crow

              digitalmythology wrote:

              void CInvenStoryDlg::OnButtonNewitem() { // TODO: Add your control notification handler code here CInvenStoryDlg *dlg = new c (/*... construction args ... */);

              Is this right, in that you are creating another instance of CInvenStoryDlg?

              digitalmythology wrote:

              I know I should use DetroyWindow() for modeless dialogs, as oppossed to EndDialog() for Modal ones, but where in the world do I actually PUT that code?

              At the point in which you are done with the modeless dialog.

              digitalmythology wrote:

              ...but not in a child dialog.

              Which means what exactly?


              "Approved Workmen Are Not Ashamed" - 2 Timothy 2:15

              "Judge not by the eye but by the heart." - Native American Proverb

              D Offline
              D Offline
              digitalmythology
              wrote on last edited by
              #6

              Not creating another instance, but rather creating a child dialog [of which I am clueless]. I prefer dto do a modeless version, but want to see Modal code as well. And thanks for the tip on "at the point of when you are through with it", but what I meant was where does the code GO? Probably CSomeClassName::OnDestroy(); but could you "dumb it down" for me? does code to destroy a child dialog go in the .cpp file for that dialog, or the .cpp file for the main dialog [parent that calls it]? "Not in a child dialog" means : that any code I write, [which is correct], will execute properly when used in the main dialog and by clicking the appropriate button on the main dialog. The exact same code will not work for a button on any of the child dialogs, except for the default AboutBox that MSVC6 included in the project. My guess is that I am not construction method[s] are wrong for the child dialogs that I made. Thanks for the help .... message me on Y! at digitalmythogy

              -digitalmythology -dm

              D 1 Reply Last reply
              0
              • D digitalmythology

                Reposting my own code is not much of a help. I need code that shows how to do it correctly. 'A modeless dialog needs to be a member variable. Otherwise, you've thrown a pointer to the wind, and have no way to do anything with it from there on in." I've heard of this solution before, but how do I actually DO it? Just saying it works for most people, but , as I" am a tactical person at times, I need to see actual code. What variable type does the dialog box need to be? CString, int, BOOL, or what? Apologies if I sound crabby, but there are millions pf sites out there on the 'ignorant net', and most issues I can resolve with research, trial, and error. However, there occassionally comes an issue to which everybody has an answer, but nobody wants to show how its actually done. What's the big M$DN secret on these subjects? Thanks, again. I really appreciate it.

                -digitalmythology -dm

                C Offline
                C Offline
                Christian Graus
                wrote on last edited by
                #7

                digitalmythology wrote:

                most issues I can resolve with research, trial, and error.

                I find that difficult to believe, in light of this comment:

                digitalmythology wrote:

                What variable type does the dialog box need to be?

                digitalmythology wrote:

                However, there occassionally comes an issue to which everybody has an answer, but nobody wants to show how its actually done

                Sometimes, things are so simple, we assume that an explanation is enough. If you created a CMyDlg * locally, what would your member variable be, if not a CMyDlg * ? What else COULD it be ? A member variable, of the right type. Set to NULL in your constructor. Set to a value when you show it. Reset to NULL and deleted both in your destructor and the code which hides it otherwise.

                Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog

                1 Reply Last reply
                0
                • D digitalmythology

                  Not creating another instance, but rather creating a child dialog [of which I am clueless]. I prefer dto do a modeless version, but want to see Modal code as well. And thanks for the tip on "at the point of when you are through with it", but what I meant was where does the code GO? Probably CSomeClassName::OnDestroy(); but could you "dumb it down" for me? does code to destroy a child dialog go in the .cpp file for that dialog, or the .cpp file for the main dialog [parent that calls it]? "Not in a child dialog" means : that any code I write, [which is correct], will execute properly when used in the main dialog and by clicking the appropriate button on the main dialog. The exact same code will not work for a button on any of the child dialogs, except for the default AboutBox that MSVC6 included in the project. My guess is that I am not construction method[s] are wrong for the child dialogs that I made. Thanks for the help .... message me on Y! at digitalmythogy

                  -digitalmythology -dm

                  D Offline
                  D Offline
                  David Crow
                  wrote on last edited by
                  #8

                  digitalmythology wrote:

                  Not creating another instance...

                  But you are in a CInvenStoryDlg method when you create another instance of CInvenStoryDlg. Is that your intention?

                  digitalmythology wrote:

                  ...what I meant was where does the code GO?

                  You call DestroyWindow() whenever you are done with the modeless dialog. It's up to you to determine where that is.

                  digitalmythology wrote:

                  My guess is that I am not construction method[s] are wrong for the child dialogs that I made.

                  I suspect this is true. Notice how the About box has its own CDialog-derived class.


                  "Approved Workmen Are Not Ashamed" - 2 Timothy 2:15

                  "Judge not by the eye but by the heart." - Native American Proverb

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups