3
votes

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;
}
1
Everything seam to be correct. Writing a sample from scratch works for me. Setting the focus only works, when OnInitDialog returns FALSE! Otherwise the dialog handler does the rest. Do ypu call DoModal? Do you have a PreTranslateMessage routine by your own? Did you overwrite PreTranslateInput?xMRi
returning NULL in OnInitDialog did the trick on setting the initial focus to one of the edit boxes, thanks. but i still cannot move the focus via tab key ;-( yes, i do call DoModal to show up the dialog but i did not override PreTranslateMessages, neither in the CDialog class nor in the CWnd derived one. i did not override PreTranslateInput as wellsuriel
Did you tried my sample and checked for differences?xMRi
not yet. hope i can do it tomorrowsuriel

1 Answers

0
votes

menu->format->tab Order(ctrl + d) this thing provide the tab order of your dialog. once you set your order click outside the dialog.i hope it will help