Focus does not change when tab key is pressed in nested CWnd-Derived class
Environment: VS2013, MFC, C ++
I have a dialog generated with CDialog with 2 static buttons (OK, Cancel), created with the dialog editor. In addition, the dialog should contain a dynamically created instance of the CWnd-Derived class, which contains 2 edit fields.
The problem is that I cannot move focus with the tab key between edit boxes, and I also cannot get one of the boxes to have the original focus when the dialog is opened. When I press the tab key, the first edit box becomes focused, but from that point I cannot move the focus with the tab key (mouse clicking works).
I already created a CWnd with WS_EX_CONTROLPARENT style but it still doesn't work to move focus. So where is the problem? What I have done so far:
//the CDialog-class which should be the container for the CWnd
//.h
class CDlgSelCatalogItem : public CDialog {
CListFilterInput _ctrlList; //CWnd-Derived, contains 2 Edit-Boxes
}
//.cpp
BOOL CDlgSelCatalogItem::OnInitDialog()
{
CRect rectList(10, 10, 100, 50);
_ctrlList.Create(rectList, this);
}
//the CWnd-derived class that contains 2 edit-boxes
//.h
class CListFilterInput : public CWnd {
BOOL Create(const RECT& rect, CWnd* pParentWnd);
//2 Edit-Boxes
CEdit _ctrl1;
CEdit _ctrl2;
}
BOOL CListFilterInput::Create(const RECT &rect, CWnd *pParentWnd)
{
BOOL bRetVal;
bRetVal = CWnd::CreateEx(WS_EX_CONTROLPARENT, NULL, _T(""), WS_CHILDWINDOW | WS_VISIBLE,
rect, pParentWnd, CTRL_ID_THIS);
if (bRetVal == TRUE){
//1st box
CRect rectTextbox = ...; //calculate rect fox box
bRetVal = _ctrl1.Create(
WS_CHILDWINDOW | WS_VISIBLE | WS_TABSTOP | ES_LEFT | ES_AUTOHSCROLL,
rectTextbox, this, CTRL_ID_TEXTBOX);
//2nd box above 1st
rectTextbox.MoveToY(rectTextbox.top - rectTextbox.Height());
bRetVal = _ctrl1.Create(
WS_CHILDWINDOW | WS_VISIBLE | WS_TABSTOP | ES_LEFT | ES_AUTOHSCROLL,
rectTextbox, this, CTRL_ID_TEXTBOX+1);
//set input-focus initially on 1st textbox - doesnt work
_ctrl1.SetFocus();
}
return bRetVal;
}
+3
source to share