Share via


FormView in MDI Tabbed Style MFC Application doesn’t restore Cursor Focus Correctly on the Reactivation of Tabs

 

If you find In an MFC Tabbed Application with a FormView, the cursor focus switches to an expected control when the Tab is reactivated, please be advised that the issue is due to a Design Flaw in MFC to handle Messages for a a FormView

 

1. To reproduce the issue, create an MDI Tabbed dialog with a Formview and have two edit field as below

clip_image001[4]

 

2. Add an event handler for Edit2 to display a MessageBox when the text is changed

clip_image002[4]

3. Dismiss the message box and the cursor will come back to the edit box being modified

4. Now Select the other edit box so that the cursor will be moved to that field

5. Now click on the Tab, the cursor will move the edit box that was displaying the dialog box, instead of the one had the cursor focus recently

 

The issue essentially happens because FormView can remember only the last active control when it is being deactivated. So when we select a new control it doesn’t store the new control as last active control, unless the FormView itself is deactivated. In the repro steps provided above, when the MessageBox is displayed the FormView gets deactivated and it stores that control as the last control. When we select the new control, the FormView is NOT deactivated and hence it won’t store it as the last activated control and when we re-activate the Formview it goes to the last Active control ‘remembered’ by the FormView.

 

To fix this issue it requires a major architectural change in MFC. So it was decided Not To Fix this issue in a hotfix or any major release

 

One work around for the above scenario could be calling the method CFormView::SaveFocusControl() in the SetFocus() handler of Edit Controls Like below

 

 

void CTestFocus2View::OnEnSetfocusEdit1()

{

       CFormView::SaveFocusControl();

}