Passing values between dialog boxes in mfc - c++

When using MFC, if i have a main dialog box, then I another dialog box is called from the main, what message is sent to the main dialogue box to let it know it has focus, is it WM_SETFOCUS()? If so, what paramaters are needed? The problem I have is, a value is selected in the child dialog and I want it copied to an edit control in the main dialog box once it (the child dialog) closes. Right now, I have it so the second dialog box copies its value to a global variable, but once the second dialog box closes, I wanted to the main dialog box to grab the global variable and display in the edit control.

You can also use a member variable in the child dialog box, like
CChildDialogBox dlg;
if (dlg.DoModal() == IDOK) // child dialog saves the value in a CString member variable m_str
{ GetDlgItem(IDC_EDIT1)->SetWindowText(dlg.m_str);
}
This MSDN article describes how you can set up member variables connected to controls in a dialog box.

I realized my problem, really a beginner's mistake, I though after a DoModal call a function would immediately exit. I didn't know I could perform additional code(assigning the edit control variable a new value and then SetWindowText) after the call, before the function ended.

Related

C++ MFC create new dialog and add Combobox item

I'm using MFC to build an application with two dialogs.
When I press a button in the parent dialog a new window including a Combobox should apprear.
I created a first dialog with a button "New". This button will open the second dialog.
Therefor I created a second dialog with a Combobox. The Combobox has a linked variable variableCombobox. The second class is called CSecond.
Before I do anything in the new dialog I want to add an item to the Combobox.
In the first dialog class I create the new window like this:
void CFirstDlg::OnBnClickedNew()
{
CSecond dlg2 = new CSecond();
dlg2.variableCombobox.AddString(L"test");
dlg2.DoModal();
}
The program crashes in the line I want to add the test string to the Combobox showing an assertion error.
I noticed that the dlg2 object is null but I don't know why.
Can anyone tell me how to create a second window immediately adding an new item in the Combobox of the second window?
The problem is the second dialog is a modal dialog. The windows does not exist before the call to DoModal() and no longer exist after that function returns. Therefore calling AddString on the combobox is not all right, since the combo box does not exist at that time.
The solution is to initialize the dialog with your desired values (like in the constructor for instance, or other methods) and then in OnInitDialog() use those values to setup the controls (including this call to AddString for the combobox).

Can't return focus to the main window after showing dialog

I create dialog window in MFC using CWnd::DoModal function.
The window is instantiated in CWinApp::OnIdle()
CPatientFile pf;
pf.DoModal();
When the DoModal function returns the focus is not returned to the main window.
I tried to set focus manually
AfxGetMainWnd()->SetFocus();
Also tried to set the main window as foreground or active.
Generally I have touch screen, so when I close the Dialog I need to press the button on the main window to get it work.
So what is the right way to do it?
Just check the constructor of CPatientFile. It may be accepting the parent window CWnd *. Pass the main window as the parent.
Like
CPatientFile pf(this); //if this code is in main window class itelf
or
CPatientFile pf(AfxGetMainWnd());
Updated:
If you have instantiated the dialog from the main window, then the focus will automatically go back to the window when the dialog is closed. I am suspecting that you have instantiated the dialog from the app class itself (CWinApp) after creating the main window. If this is the case then the main window may not get the focus & you must create the dialog from within the main window. Or if you are creating the dialog in a separate thread.
As a work around you can use AfxGetMainWnd()->SetForegroundWindow() or AfxGetMainWnd()->SetActiveWindow(). But first I would try to find the cause of the issue & try to write better code (like suggested in above paragraph).
If you have copy-pasted, the statement is itself incorrect:
CPatientFile pf();
The next line shouldn't compile at all. Why? Because it is declaring a function named pf which returns CPatientFile.
If that is correct, may I ask if there is any multithreading involved. Is another thread creating a window?
How do you check if parent windows has (not) got the focus?

Child's DoDataExchange is never called in child dialog ? - MFC

I simply want to open a child dialog and have it print a result from the parent dialog in one of the child's static text controls. Using breakpoints I noticed that both DoDataExchange and my overloaded OnInitDialog are never called in the child so the static text control crashes any time i try to print something to it. The child dialog does display itself fine otherwise.
What could be the problem ?
//in parent.cpp
CResultsDlg childResultsDlg = this;
childResultsDlg.DoModal(15.7); //overloaded to pass value to a child member var
more information from my previous post
Child Dialog - SetWindowTextA or SendMessageA crashes program - MFC
All of the basics of MFC are covered in the SCRIBBLE tutorial.
http://msdn.microsoft.com/en-us/library/f35t8fts(v=vs.90).aspx
An example of passing data into a dialog and displaying that data in dialog controls is provided in the tutorial with the CPenWidthsDlg.

calling Dialog from property page / sheet

I have a property page application. In it, created a dialog called mydlg which is inherited from CDialog.
In the first property page, in its OnInitDialog i tried to launch the mydlg like this:
mydlg m;
m.DoModal();
but it crashes.
If I move the two lines to the Initinstace in mypropertysheet, it works, the dialog launches. What is the proper way of calling the DoModal in the both places?.
Seconly, how to read the content of edit box on the mydlg from inside the mypropertysheet initinstance using the ID of the edit box.
After DoModal returns the edit box has been destroyed, so you cannot read it using the ID of the edit box. The mydlg class should read the edit box (usually in OnOK) and put the text into a mydlg member variable. After DoModal returns you can still read the mydlg member variable.

Receiving results from dialogs

I am trying to get some data from a dialog in an MFC C++ dialog-based application. I ahev made this image to help you understand better my situation:
When the user clicks the findWndBtn in the left-side window, the right-side dialog appears by dlg.DoModal() function. After the user completes the fields in the right-side the dialog, the following code is executed:
HWND WindowHandle = FindWindow( WindowClass, WindowName );
CDialogEx::OnOk();
After the dialog exits, I want the m_myWndHwnd field to pe filled with the handle found by that dialog.
How can I do that?
Tell me please, If didn't said enough, for me to complete with the detail that you need.
Two options:
Have the second dialog post a message back to the first dialog, passing the result you want to store as an argument.
Have the second dialog store the result in a class member variable, and have the caller retrieve it (via a member function or directly if you're OK with that) when the dialog returns.