Click here to Skip to main content
15,740,797 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I created a dialog window in MFC VC++. in that, I inserted a button to open another dialog window. I want to perform some tasks in the second dialog based on the variables involved in first dialog class. So how can I get access of that in the second dialag class.

What I have tried:

I couldn't achieve even though I tried the following ways.
1)I declared all the parameters as public which I require
2)I inherited that first dialog class along with CDialog class into the second dialog class, but there were build errors.
Please help me. It will be very useful for my project.
Updated 19-May-20 4:24am
Richard MacCutchan 18-Feb-19 8:15am    
Create a method in the parent which can return the data that the child needs. Alternatively, add the required data to the constructor of the child dialog. Either of those is better than exposing your variables.

1 solution

You can do that easily. The 2nd dialog is opened by calling DoModal()[^] but before calling this function you can assign values to any public member variables in the 2nd Dialog class.
Let's assume the 2nd Dialog has "firstmarameter" and "another parameter" as member variables. We can assume the first Dialog also has a member variable named 'data', so you can see how to assign values from one dialog to another:

void CPrimary1Dlg::OnBnClickedSecondBtn()
	CSecondDlg Dlg;
	Dlg.firstparameter = data;  // assign a value from the 1st dialog to the 2nd 
        Dlg.anotherparameter = "This is my new dialog"; 
// assign a value to the 2nd dialog

	Dlg.DoModal(); // Launch the 2nd dialog
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900