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
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Passing data from modal dialog.

Passing data from modal dialog.

Scheduled Pinned Locked Moved C / C++ / MFC
11 Posts 4 Posters 0 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.
  • S Offline
    S Offline
    SzyKam
    wrote on last edited by
    #1

    Hello! I've made a simple application. When I click a button in mian window a modal popup window shows up: Ustawienia modalne; modalne.DoModal(); I want that when I click OK in this modal wnd. it changes a Static Text in main wnd.: void Modalne::OnBnClickedOk() { UpdateData(true); ((CNoweOknoDlg*)m_pParent)->m_text1 = "tekst"; ((CNoweOknoDlg*)m_pParent)->UpdateData(false); OnOK(); } When I press this button the app crasches :( When I made this popup wnd. as modeless using Create() funtion everything is OK. :/

    C E 2 Replies Last reply
    0
    • S SzyKam

      Hello! I've made a simple application. When I click a button in mian window a modal popup window shows up: Ustawienia modalne; modalne.DoModal(); I want that when I click OK in this modal wnd. it changes a Static Text in main wnd.: void Modalne::OnBnClickedOk() { UpdateData(true); ((CNoweOknoDlg*)m_pParent)->m_text1 = "tekst"; ((CNoweOknoDlg*)m_pParent)->UpdateData(false); OnOK(); } When I press this button the app crasches :( When I made this popup wnd. as modeless using Create() funtion everything is OK. :/

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

      You should expose the string on your dialog and set the text in your main window. Apart from anything else, your approach makes the two forms tightly coupled for no good reason.

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

      S 1 Reply Last reply
      0
      • C Christian Graus

        You should expose the string on your dialog and set the text in your main window. Apart from anything else, your approach makes the two forms tightly coupled for no good reason.

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

        S Offline
        S Offline
        SzyKam
        wrote on last edited by
        #3

        Any more details?

        C 1 Reply Last reply
        0
        • S SzyKam

          Any more details?

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

          Which bit are you confused by ? Expose a parameter to store the value you want to return, do the rest in your main dialog if the modal call returns OK.

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

          S 1 Reply Last reply
          0
          • C Christian Graus

            Which bit are you confused by ? Expose a parameter to store the value you want to return, do the rest in your main dialog if the modal call returns OK.

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

            S Offline
            S Offline
            SzyKam
            wrote on last edited by
            #5

            m_text1 is a CString value that represents static text in mian window. To change it I have to call this: ((CNoweOknoDlg*)m_pParent)->m_text1 = "text" on button click. I don't have any idea how to do this in a different way. I'm confused why it works with modeless window.

            C S 2 Replies Last reply
            0
            • S SzyKam

              m_text1 is a CString value that represents static text in mian window. To change it I have to call this: ((CNoweOknoDlg*)m_pParent)->m_text1 = "text" on button click. I don't have any idea how to do this in a different way. I'm confused why it works with modeless window.

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

              OK, so you didn't understand anythign I said ? I'm not sure why it doesn't work, but either way, when class 1 needs to know about the internals of class 2, they are tightly coupled, and thus cannot work without each other. You're doing this in your OnOK method, so I assume it happens as the dialog closes. so, what you want to do is, the code in CNowkOKnoDlg, your main class, will create this child form and show it. Assuming that "text" is going to become a variable, add a public string variable, then set m_text1 inside the class that holds that variable, and created the dialog that sets it.

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

              S 1 Reply Last reply
              0
              • S SzyKam

                Hello! I've made a simple application. When I click a button in mian window a modal popup window shows up: Ustawienia modalne; modalne.DoModal(); I want that when I click OK in this modal wnd. it changes a Static Text in main wnd.: void Modalne::OnBnClickedOk() { UpdateData(true); ((CNoweOknoDlg*)m_pParent)->m_text1 = "tekst"; ((CNoweOknoDlg*)m_pParent)->UpdateData(false); OnOK(); } When I press this button the app crasches :( When I made this popup wnd. as modeless using Create() funtion everything is OK. :/

                E Offline
                E Offline
                erfi
                wrote on last edited by
                #7

                if you want to change the text of a control you can do this :

                void CMdlDlg::OnOkButton()
                {
                CWnd *parent = this->GetParent();
                parent->SetDlgItemText(" control ID ","new text");
                }

                or if you want to change the main dialog tilte then do this :

                void CMdlDlg:;OnOkButton()
                {
                (this->GetParent())->SetWindowText("new text");
                }

                1 Reply Last reply
                0
                • C Christian Graus

                  OK, so you didn't understand anythign I said ? I'm not sure why it doesn't work, but either way, when class 1 needs to know about the internals of class 2, they are tightly coupled, and thus cannot work without each other. You're doing this in your OnOK method, so I assume it happens as the dialog closes. so, what you want to do is, the code in CNowkOKnoDlg, your main class, will create this child form and show it. Assuming that "text" is going to become a variable, add a public string variable, then set m_text1 inside the class that holds that variable, and created the dialog that sets it.

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

                  S Offline
                  S Offline
                  S Douglas
                  wrote on last edited by
                  #8

                  Christian Graus wrote:

                  Assuming that "text" is going to become a variable, add a public string variable, then set m_text1 inside the class that holds that variable, and created the dialog that sets it

                  Christian I think even I am misunderstanding you. Are you suggesting something of the following nature?

                  CFoo dlg
                  
                  Dlg.m_text1 = “the test string”
                  Dlg.Domodal();
                  

                  Isn’t is safer to CFoo dlg /* SetMyText Really is a public method, this allows any necessary business rules to be handled by the CFoo class, internally */ Dlg.SetMyText(“the test string”); Dlg.Domodal();


                  I'd love to help, but unfortunatley I have prior commitments monitoring the length of my grass. :Andrew Bleakley:

                  C 1 Reply Last reply
                  0
                  • S S Douglas

                    Christian Graus wrote:

                    Assuming that "text" is going to become a variable, add a public string variable, then set m_text1 inside the class that holds that variable, and created the dialog that sets it

                    Christian I think even I am misunderstanding you. Are you suggesting something of the following nature?

                    CFoo dlg
                    
                    Dlg.m_text1 = “the test string”
                    Dlg.Domodal();
                    

                    Isn’t is safer to CFoo dlg /* SetMyText Really is a public method, this allows any necessary business rules to be handled by the CFoo class, internally */ Dlg.SetMyText(“the test string”); Dlg.Domodal();


                    I'd love to help, but unfortunatley I have prior commitments monitoring the length of my grass. :Andrew Bleakley:

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

                    Yes, you are misunderstanding me. Or I'm misunderstanding you. I thought you were setting a string in the main window from the modal dialog. I am saying, set the string in the main window, and expose a property on the dialog to tell you what the string is. A get method makes more sense than a public property, that much is true. I was just trying to keep it simple.

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

                    S 1 Reply Last reply
                    0
                    • C Christian Graus

                      Yes, you are misunderstanding me. Or I'm misunderstanding you. I thought you were setting a string in the main window from the modal dialog. I am saying, set the string in the main window, and expose a property on the dialog to tell you what the string is. A get method makes more sense than a public property, that much is true. I was just trying to keep it simple.

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

                      S Offline
                      S Offline
                      S Douglas
                      wrote on last edited by
                      #10

                      Christian Graus wrote:

                      I thought you were setting a string in the main window from the modal dialog. I am saying, set the string in the main window, and expose a property on the dialog to tell you what the string is.

                      Wasn't me, I was just bored perusing the message boards noticed the question and which made no sense (at least to me) :-O Posting a string back a parent dialog is always a pain, so far I have always found it better to just wait until the modal child closes then follow what you said and use a getter. Shrugs, lol not really my problem I was just confused by the question and responses. :)


                      I'd love to help, but unfortunatley I have prior commitments monitoring the length of my grass. :Andrew Bleakley:

                      1 Reply Last reply
                      0
                      • S SzyKam

                        m_text1 is a CString value that represents static text in mian window. To change it I have to call this: ((CNoweOknoDlg*)m_pParent)->m_text1 = "text" on button click. I don't have any idea how to do this in a different way. I'm confused why it works with modeless window.

                        S Offline
                        S Offline
                        S Douglas
                        wrote on last edited by
                        #11

                        What I believe Christian is trying to say here is do something to the following affect. CFoo Chlddlg /* GetMyText Really is a public method, this allows any necessary business rules to be handled by the CFoo class, internally */ if(IDOK == Chlddlg.Domodal()) { CString str = Chlddlg.GetMyText(); SetWindowText(str); } Dont try and post the string back to the parent dialog, let the parent collect it from the child. This protects the inner workings of both classes. GetMyText() could return any variation of data. (this is just a simple example).


                        I'd love to help, but unfortunatley I have prior commitments monitoring the length of my grass. :Andrew Bleakley:

                        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