Changing the tab order of a control at runtime!

Follow these steps(Just a demo)…

  1. Add two buttons to a fresh dialog in a sequencial order, IDC_BUTTON1 and IDC_BUTTON2
  2. Goto OnInitDialog(you are not restricted to OnInitDialog, you can also do this on a button click) and paste this line of code

[sourcecode language=’cpp’]GetDlgItem( IDC_BUTTON1 )->SetWindowPos(GetDlgItem( IDC_BUTTON2 ),
0, 0, 0, 0,
SWP_NOSIZE | SWP_NOMOVE | SWP_NOACTIVATE );[/sourcecode]

So now run the dialog and you will see that the focus is on the second button i.e. IDC_BUTTON2, I am invoking SetWindowPos on IDC_BUTTON1 and hwndInsertAfter is IDC_BUTTON2 hence IDC_BUTTON1 is moved down in the Z-Order after IDC_BUTTON2. So this is how we dynamically change the Z-Order or tab order.

The key to all this behavior is because SWP_NOZORDER is not given when SetWindowPos is called, so it changes Z-Order of the control on which this method is invoked.

3 thoughts on “Changing the tab order of a control at runtime!

Appreciate your comments...