Passing data from modal dialog.
-
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. :/
-
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. :/
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
-
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
-
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
-
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
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.
-
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.
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
-
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. :/
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");
} -
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
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:
-
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:
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
-
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
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:
-
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.
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: