Home > MFC, Windows API > Changing the tab order of a control at runtime!

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
GetDlgItem( IDC_BUTTON1 )->SetWindowPos(GetDlgItem( IDC_BUTTON2 ),
                                           0, 0, 0, 0,
                                           SWP_NOSIZE | SWP_NOMOVE | SWP_NOACTIVATE );

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.

Advertisements
  1. prashant singh
    November 20, 2009 at 6:36 pm

    this code is not fit for tab sequence.

  1. August 26, 2008 at 7:11 am

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: