Tuesday, March 22, 2011

Change controls tab sequence

It is simple to set tab sequence of a dialog controls in Visual Studio at design time. To do it just press Ctrl+D, when the form designer is active, or use corresponding menu item from Format menu.

To change tab order at run-time you can use SetWindowPos function. Assume we have some control with id IDC_STATIC and another one, which was created dynamically (IDC_DYNAMIC). We need set dynamic control the next after IDC_STATIC. It can be done with like the following:

HWND hNext = ::GetNextWindow(GetDlgItem(IDC_STATIC)->GetSafeHwnd(), GW_HWNDNEXT);
::SetWindowPos(GetDlgItem(IDC_DYNAMIC)->GetSafeHwnd(), GetDlgItem(IDC_STATIC)->GetSafeHwnd(), 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE );
::SetWindowPos(hNext, GetDlgItem(IDC_DYNAMIC)->GetSafeHwnd(), 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE );

But if you want add child dialog to your form the method above doesn't work. Tab sequences of the parent and child window will be independent. The easiest way in such case is to add the following line in the child dialog implementation class after child dialog creation:

ModifyStyleEx(0, WS_EX_CONTROLPARENT);

No comments: