title: mfc tab control switch tabs
body: <p>I created a simple tab control that has 2 tabs (each tab is a different dialog). The thing is that i don't have any idea how to switch between tabs (when the user presses <code>Titlu Tab1</code> to show the dialog i made for the first tab, and when it presses <code>Titlu Tab2</code> to show my other dialog). I added a handler for changing items, but i don't know how should i acces some kind of index or child for tabs.</p>
  
  <p><code>Tab1.h</code> and <code>Tab2.h</code> are headers for dialogs that show only static texts with the name of the each tab.</p>
  
  <p>There may be an obvious answer to my question, but i am a real newbie in c++ and MFC.</p>
  
  <p>This is my header:</p>
  
  <pre><code>// CTabControlDlg.h : header file
  //
  
  #pragma once
  #include "afxcmn.h"
  #include "Tab1.h"
  #include "Tab2.h"
  
  
  // CCTabControlDlg dialog
  class CCTabControlDlg : public CDialog
  {
  // Construction
  public:
     CCTabControlDlg(CWnd* pParent = NULL);   // standard constructor
  
  // Dialog Data
  enum { IDD = IDD_CTABCONTROL_DIALOG };
  
  protected:
  virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
  
  
  // Implementation
  protected:
     HICON m_hIcon;
  
    // Generated message map functions
    virtual BOOL OnInitDialog();
    afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
    afx_msg void OnPaint();
    afx_msg HCURSOR OnQueryDragIcon();
    DECLARE_MESSAGE_MAP()
  public:
    CTabCtrl m_tabcontrol1;
  
    CTab1 m_tab1;
    CTab2 m_tab2;
    afx_msg void OnTcnSelchangeTabcontrol(NMHDR *pNMHDR, LRESULT *pResult);
  };
  </code></pre>
  
  <p>And this is the .cpp:</p>
  
  <pre><code>// CTabControlDlg.cpp : implementation file
  //
  
  #include "stdafx.h"
  #include "CTabControl.h"
  #include "CTabControlDlg.h"
  
  #ifdef _DEBUG
  #define new DEBUG_NEW
  #endif
  
  
  // CAboutDlg dialog used for App About
  
  class CAboutDlg : public CDialog
  {
   public:
     CAboutDlg();
  
   // Dialog Data
    enum { IDD = IDD_ABOUTBOX };
  
    protected:
    virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
  
   // Implementation
   protected:
      DECLARE_MESSAGE_MAP()
   };
  
   CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
   {
   }
  
   void CAboutDlg::DoDataExchange(CDataExchange* pDX)
   {
     CDialog::DoDataExchange(pDX);
   }
  
   BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
   END_MESSAGE_MAP()
  
  
   // CCTabControlDlg dialog
  
  
  
  
   CCTabControlDlg::CCTabControlDlg(CWnd* pParent /*=NULL*/)
     : CDialog(CCTabControlDlg::IDD, pParent)
   {
     m_hIcon = AfxGetApp()-&gt;LoadIcon(IDR_MAINFRAME);
   }
  
   void CCTabControlDlg::DoDataExchange(CDataExchange* pDX)
   {
    CDialog::DoDataExchange(pDX);
    DDX_Control(pDX, IDC_TABCONTROL, m_tabcontrol1);
   }
  
   BEGIN_MESSAGE_MAP(CCTabControlDlg, CDialog)
    ON_WM_SYSCOMMAND()
    ON_WM_PAINT()
    ON_WM_QUERYDRAGICON()
    //}}AFX_MSG_MAP
    ON_NOTIFY(TCN_SELCHANGE, IDC_TABCONTROL,         &amp;CCTabControlDlg::OnTcnSelchangeTabcontrol) 
      END_MESSAGE_MAP()
  
  
     // CCTabControlDlg message handlers
  
    BOOL CCTabControlDlg::OnInitDialog()
    {
      CDialog::OnInitDialog();
  
      // Add "About..." menu item to system menu.
  
      // IDM_ABOUTBOX must be in the system command range.
      ASSERT((IDM_ABOUTBOX &amp; 0xFFF0) == IDM_ABOUTBOX);
      ASSERT(IDM_ABOUTBOX &lt; 0xF000);
  
      CMenu* pSysMenu = GetSystemMenu(FALSE);
      if (pSysMenu != NULL)
  {
      CString strAboutMenu;
      strAboutMenu.LoadString(IDS_ABOUTBOX);
      if (!strAboutMenu.IsEmpty())
      {
          pSysMenu-&gt;AppendMenu(MF_SEPARATOR);
          pSysMenu-&gt;AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
      }
  }
  
  // Set the icon for this dialog.  The framework does this automatically
  //  when the application's main window is not a dialog
  SetIcon(m_hIcon, TRUE);         // Set big icon
  SetIcon(m_hIcon, FALSE);        // Set small icon
  
  // TODO: Add extra initialization here
  
  CTabCtrl* pTabCtrl = (CTabCtrl*)GetDlgItem(IDC_TABCONTROL);
  
  m_tab1.Create(IDD_TAB1, pTabCtrl);
  
  TCITEM item1;
  item1.mask      = TCIF_TEXT | TCIF_PARAM;
  item1.lParam    = (LPARAM)&amp; m_tab1;
  item1.pszText   = _T("Titlu Tab1");
  pTabCtrl-&gt;InsertItem(0, &amp;item1);
  
  //Pozitionarea dialogului
  CRect rcItem;
  pTabCtrl-&gt;GetItemRect(0, &amp;rcItem);
  m_tab1.SetWindowPos(NULL, rcItem.left, rcItem.bottom + 1, 0, 0, SWP_NOSIZE | SWP_NOZORDER );
  
  m_tab1.ShowWindow(SW_SHOW);
  
  // al doilea tab
  m_tab2.Create(IDD_TAB2, pTabCtrl);
  TCITEM item2;
  item2.mask      = TCIF_TEXT | TCIF_PARAM;
  item2.lParam    = (LPARAM)&amp; m_tab1;
  item2.pszText   = _T("Titlu Tab2");
  pTabCtrl-&gt;InsertItem(0, &amp;item2);
  
  //Pozitionarea dialogului
  //CRect rcItem;
  pTabCtrl-&gt;GetItemRect(0, &amp;rcItem);
  m_tab2.SetWindowPos(NULL, rcItem.left, rcItem.bottom + 1, 0, 0, SWP_NOSIZE | SWP_NOZORDER );
  
  m_tab2.ShowWindow(SW_SHOW);
  
  return TRUE;  // return TRUE  unless you set the focus to a control
    }
  
    void CCTabControlDlg::OnSysCommand(UINT nID, LPARAM lParam)
    {
  if ((nID &amp; 0xFFF0) == IDM_ABOUTBOX)
  {
      CAboutDlg dlgAbout;
      dlgAbout.DoModal();
  }
  else
  {
      CDialog::OnSysCommand(nID, lParam);
  }
      }
  
    // If you add a minimize button to your dialog, you will need the code below
    //  to draw the icon.  For MFC applications using the document/view model,
    //  this is automatically done for you by the framework.
  
    void CCTabControlDlg::OnPaint()
    {
  if (IsIconic())
  {
      CPaintDC dc(this); // device context for painting
  
      SendMessage(WM_ICONERASEBKGND, reinterpret_cast&lt;WPARAM&gt;(dc.GetSafeHdc()),   0);
  
      // Center icon in client rectangle
      int cxIcon = GetSystemMetrics(SM_CXICON);
      int cyIcon = GetSystemMetrics(SM_CYICON);
      CRect rect;
      GetClientRect(&amp;rect);
      int x = (rect.Width() - cxIcon + 1) / 2;
      int y = (rect.Height() - cyIcon + 1) / 2;
  
      // Draw the icon
      dc.DrawIcon(x, y, m_hIcon);
  }
  else
  {
      CDialog::OnPaint();
  }
    }
  
    // The system calls this function to obtain the cursor to display while the user drags
    //  the minimized window.
    HCURSOR CCTabControlDlg::OnQueryDragIcon()
    {
     return static_cast&lt;HCURSOR&gt;(m_hIcon);
    }
  
  
    void CCTabControlDlg::OnTcnSelchangeTabcontrol(NMHDR *pNMHDR, LRESULT *pResult)
    {
  // TODO: Add your control notification handler code here
  *pResult = 0;
  
     }
  </code></pre>