CTabCtrl as selection control
I kow usually a tab control is used to select and display different child dialog windows, or "pages". But how can I use it as a selection control, like a drop list. I would have some edit boxes "inside" the tab control as one page. The page will not change, and these editboxes will remain, but the data in them will change depending on which tab is clicked. Is this possible?
Or if there's a similar control used for selection that's laid out horizontally, I could use that too.
# 1 Re: CTabCtrl as selection control
Leverage DDX and create some control and value variables. For the tab control, right click on the tab control in the resource editor and choose 'Add variable', in the wizard enter 'm_TabCtrl' for the name and press finish. Next do the same for the edit control except instead of a control variable, create a 'value' variable by changing the 'Category' type from 'control' to 'value'. By default the variable type will be CString but you can make it an int or whatever. Any just leave it as CString and enter m_sEdit for the name.
You should end up with the following in DoDataExchange( )
void CTabDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
DDX_Control(pDX, IDC_TAB1, m_TabCtrl);
DDX_Text(pDX, IDC_EDIT1, m_sEdit);
}
Next add some tabs in InitDialog.
BOOL CTabDlg::OnInitDialog()
{
CDialog::OnInitDialog();
SetIcon(m_hIcon, TRUE);
SetIcon(m_hIcon, FALSE);
// Add some tabs to the control
m_TabCtrl.InsertItem( 0, _T("1st one") );
m_TabCtrl.InsertItem( 1, _T("2nd one") );
m_TabCtrl.InsertItem( 2, _T("3rd one") );
return TRUE;
}
Finally override the tab control on select change
void CTabDlg::OnTcnSelchangeTab1(NMHDR *pNMHDR, LRESULT *pResult)
{
// Set the edit box text based on the tab selection
switch( m_TabCtrl.GetCurSel( ) )
{
case 0: m_sEdit = _T("1st tab selected"); break;
case 1: m_sEdit = _T("2nd tab selected"); break;
case 2: m_sEdit = _T("3rd tab selected"); break;
}
*pResult = 0;
// Call UpdateData to transfer the value of m_sEdit into the edit box
UpdateData( FALSE );
}
See the attached 'Tab' project.
Arjay at 2007-11-10 22:29:01 >

# 2 Re: CTabCtrl as selection control
thanks.
can you explain this part? (NMHDR *pNMHDR, LRESULT *pResult)
I know it is handled in the message map, but if I were to "virtually" push the button, ie call OnTcnSelchangeTab1() programmatically, what would I pass? I ask because, on occasion, I would want to set the selected tab like this m_TabCtrl.SetCurSel(1). But then there are no messages to activate OnTcnSelchangeTab1(), so I would need to call it manually.
# 3 Re: CTabCtrl as selection control
thanks.
can you explain this part? (NMHDR *pNMHDR, LRESULT *pResult)
I know it is handled in the message map, but if I were to "virtually" push the button, ie call OnTcnSelchangeTab1() programmatically, what would I pass? I ask because, on occasion, I would want to set the selected tab like this m_TabCtrl.SetCurSel(1). But then there are no messages to activate OnTcnSelchangeTab1(), so I would need to call it manually.No problem just move updating the edit text into another method.
void CTabDlg::UpdateTabControls( )
{
// Set the edit box text based on the tab selection
switch( m_TabCtrl.GetCurSel( ) )
{
case 0: m_sEdit = _T("1st tab selected"); break;
case 1: m_sEdit = _T("2nd tab selected"); break;
case 2: m_sEdit = _T("3rd tab selected"); break;
}
// Call UpdateData to transfer the value of m_sEdit into the edit box
UpdateData( FALSE );
}
void CTabDlg::OnTcnSelchangeTab1(NMHDR *pNMHDR, LRESULT *pResult)
{
// Update the tab controls
UpdateTabControls( );
*pResult = 0;
}
// Programmatically set the tab
void CTabDlg::SetCurTab( int nSel )
{
// Set the current tab
m_TabCtrl.SetCurSel( nSel );
// Update the tab controls
UpdateTabControls( );
}
Arjay at 2007-11-10 22:30:59 >
