Environment: VS2013, MFC, C++
I have a CDialog derived dialog with 2 static buttons (OK, Cancel), created with the dialog editor. Additionally the dialog should contain a dynamically created instance of a CWnd-Derived class, which contains 2 edit boxes.
Problem is that I cannot move the focus via tab-key between the edit boxes and I also cannot make one of the boxes have the initial focus when opening the dialog. When I hit the tab key the 1st editbox gets focussed but and from this point on I can't move the focus with the tab key away (clicking with the mouse works).
I already created the CWnd with the WS_EX_CONTROLPARENT style, but it still doesn't work to move the focus. So where's the problem? What I've 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;
}