We help IT Professionals succeed at work.

Need some help with my TabControl  (500 Poinst Grade A) please help

Thomas Stockbruegger
on
Medium Priority
275 Views
Last Modified: 2010-08-05
Hello,
I have built TabControl like this one
http://www.codeguru.com/cpp/controls/controls/tabcontrols/article.php/c5239/

How can I change the focus on my first tab?
Please be so kind to help me, thanks 500 Points
Best regards,
Thomas
(still a beginner in Visual C++)

in my ...Dlg.cpp
OnIntitial()....
//--------------------------- this works fine ------------------------------
    m_myTabCtrl.InsertItem(0, _T("Tab One"));
     m_myTabCtrl.InsertItem(1, _T("Tab Thwo"));
     m_myTabCtrl.InsertItem(2, _T("Tab Three"));
//-----------------------------------------------------------------------------    
   


 //###############################################
//This is the code from CodeGuru.com works fine

CMyTabCtrl::CMyTabCtrl()
{
 m_tabPages[0]=new CTabOne;
 m_tabPages[1]=new CTabTwo;
 m_tabPages[2]=new CTabThree;

 m_nNumberOfPages=3;
}

CMyTabCtrl::~CMyTabCtrl()
{
 for(int nCount=0; nCount < m_nNumberOfPages; nCount++){
  delete m_tabPages[nCount];
 }
}

void CMyTabCtrl::Init()
{
 m_tabCurrent=0;

 m_tabPages[0]->Create(IDD_TAB_ONE, this);
 m_tabPages[1]->Create(IDD_TAB_TWO, this);
 m_tabPages[2]->Create(IDD_TAB_THREE, this);

 m_tabPages[0]->ShowWindow(SW_SHOW);
 m_tabPages[1]->ShowWindow(SW_HIDE);
 m_tabPages[2]->ShowWindow(SW_HIDE);

 SetRectangle();
}

void CMyTabCtrl::SetRectangle()
{
 CRect tabRect, itemRect;
 int nX, nY, nXc, nYc;

 GetClientRect(&tabRect);
 GetItemRect(0, &itemRect);

 nX=itemRect.left;
 nY=itemRect.bottom+1;
 nXc=tabRect.right-itemRect.left-1;
 nYc=tabRect.bottom-nY-1;

 m_tabPages[0]->SetWindowPos(&wndTop, nX, nY, nXc, nYc, SWP_SHOWWINDOW);
 for(int nCount=1; nCount < m_nNumberOfPages; nCount++){
  m_tabPages[nCount]->SetWindowPos(&wndTop, nX, nY, nXc, nYc, SWP_HIDEWINDOW);
 }
}


//////////////////////////////////////////////////////
// CMyTabCtrl message handlers

void CMyTabCtrl::OnLButtonDown(UINT nFlags, CPoint point)
{
 CTabCtrl::OnLButtonDown(nFlags, point);

 if(m_tabCurrent != GetCurFocus()){
  m_tabPages[m_tabCurrent]->ShowWindow(SW_HIDE);
  m_tabCurrent=GetCurFocus();
  m_tabPages[m_tabCurrent]->ShowWindow(SW_SHOW);
  m_tabPages[m_tabCurrent]->SetFocus();
 }
}



//##############################################
Here is the code of my CTabOne class.
How can I change the focus from IDC_EDIT1 to IDC_EDIT2
I know on a single project I have to add this to my OnIntital() in my ...Dlg.cpp
               CWnd *pFocus = GetDlgItem(IDC_EDIT2);
      pFocus->SetFocus();
      return FALSE;

How does it work here. Each Tab-Dialog has one Class.
I tried to add a OnInitial() to my ctapone.cpp but it will not work.
Please be so kind to let me know. Thanks a lot.


// TabOne.cpp : Implementierungsdatei
//

#include "stdafx.h"
#include "Tab Control_Register_mit Klasse.h"
#include "TabOne.h"
#include ".\tabone.h"


// CTabOne-Dialogfeld

IMPLEMENT_DYNAMIC(CTabOne, CDialog)
CTabOne::CTabOne(CWnd* pParent /*=NULL*/)
      : CDialog(CTabOne::IDD, pParent)
{
      
}

CTabOne::~CTabOne()
{
}

void CTabOne::DoDataExchange(CDataExchange* pDX)
{
      CDialog::DoDataExchange(pDX);
}


BEGIN_MESSAGE_MAP(CTabOne, CDialog)
      ON_BN_CLICKED(IDC_BUTTON1, OnBnClickedButton1)
END_MESSAGE_MAP()



BOOL CTabOne::OnInitDialog()
{
      CDialog::OnInitDialog();

               CWnd *pFocus = GetDlgItem(IDC_EDIT2);
      pFocus->SetFocus();
      
      return FALSE;  // return TRUE unless you set the focus to a control
}

Comment
Watch Question

Commented:
Maybe it is better to set Tab Order in the dialog template?
How can I do this?

Commented:
In VS 2005 there is menu item Format - Tab Order. In previous versions there is also Tab Order command, but it can be placed in other menu - find it. Open dialog template, select Tab Order command, and click all dialog controls in the desired order.
BTW, it is possible to do the same manually editing .rc file. Order of controls in the dialog resource is Tab order. It is possible to change lines order there.
Okay...I understand.
I now that I can change the Tab Order in my project with Tab Order command
manually.

But I would like to change it with my code.
This one works in any ...Dlg.cpp OnInitDialog()
but why not here ?
     
CWnd *pFocus = GetDlgItem(IDC_EDIT2);
pFocus->SetFocus();
return FALSE;


Is there any connection because the focus is set here?

void CMyTabCtrl::OnLButtonDown(UINT nFlags, CPoint point)
{
 CTabCtrl::OnLButtonDown(nFlags, point);

 if(m_tabCurrent != GetCurFocus()){
  m_tabPages[m_tabCurrent]->ShowWindow(SW_HIDE);
  m_tabCurrent=GetCurFocus();
  m_tabPages[m_tabCurrent]->ShowWindow(SW_SHOW);
  m_tabPages[m_tabCurrent]->SetFocus(); <-----------------------------!!



Commented:
m_tabPages[m_tabCurrent]->SetFocus();

After this you can call some tab page function which sets initial control:

m_tabPages[m_tabCurrent]->SetInitialFocus();

void CMyTabPage::SetInitialFocus()
{
    CWnd *pFocus = GetDlgItem(IDC_EDIT2);
    pFocus->SetFocus();

    or:

    GoToDlgCtrl(GetDlgItem(IDC_EDIT2));
}
Is SetInitialFocus() just a Name from you?

I always get the Error no element of CDialog

void CMyTabCtrl::OnLButtonDown(UINT nFlags, CPoint point)
{
 CTabCtrl::OnLButtonDown(nFlags, point);

 if(m_tabCurrent != GetCurFocus())
{
  m_tabPages[m_tabCurrent]->ShowWindow(SW_HIDE);
  m_tabCurrent=GetCurFocus();
  m_tabPages[m_tabCurrent]->ShowWindow(SW_SHOW);
  m_tabPages[m_tabCurrent]->SetFocus(); <-----------------------------!!

   m_tabPages[m_tabCurrent]->SetInitialFocus();    //Error no element of CDialog
                                                                                 will not compile
   
   // just SetInitialFocus(); //           Will compile but =
                                                      Error Debug Stop in CWnd* CWnd::SetFocus()
                                                      ASSERT (::IsWindow(m_Wnd)....and so on

}
}

I just added the SetInitialFocus() as a function void/void to CMyTabCtrl.

void CMyTabCtrl::SetInitialFocus()
{
    CWnd *pFocus = GetDlgItem(IDC_EDIT2);
    pFocus->SetFocus();
}


Please let me know Alex. I very appreciate your help.
Best regards,
Thomas

Commented:
I mean - add such function to CTabOne, CTabTwo etc.
To call such function you need casting.

if (m_tabCurrent == 0 )
    ((CTabOne*)m_tabPages[m_tabCurrent])->SetInitialFocus();
if (m_tabCurrent == 1 )
    ((CTabTwo*)m_tabPages[m_tabCurrent])->SetInitialFocus();
else
    ((CTabThree*)m_tabPages[m_tabCurrent])->SetInitialFocus();

Not generic solution, but in any case the whole way is not generic, just allows to make things working.
Hi Alex,
good and bad news.
I have added your code and a function for the focus to each
class.

void CMyTabCtrl::OnLButtonDown(UINT nFlags, CPoint point)
{
 CTabCtrl::OnLButtonDown(nFlags, point);

 if(m_tabCurrent != GetCurFocus())
{
  m_tabPages[m_tabCurrent]->ShowWindow(SW_HIDE);
  m_tabCurrent=GetCurFocus();
  m_tabPages[m_tabCurrent]->ShowWindow(SW_SHOW);
  m_tabPages[m_tabCurrent]->SetFocus();

if (m_tabCurrent == 0 )
{
        ((CTabOne*)m_tabPages[m_tabCurrent])->SetInitialFocus();
}

if (m_tabCurrent == 1 )
{
      ((CTabTwo*)m_tabPages[m_tabCurrent])->SetInitialFocus();
}
  ..
  ..
  ..
}
}

Focus for m_tabCurrent==1  =>CTabTwo works
when I press the button on the tabcontrol and the 2. dialog box will show
the focus is on IDC_EDIT2 and not IDC_EDIT1...okay

Focus for m_tabCurrent==0  =>CTabOne
only works when I was first on tab two or three

I think when the program starts it is still in the main ...Dlg.cpp
and I have to press tabtwo or tabthree to go to:
 void CMyTabCtrl::OnLButtonDown(UINT nFlags, CPoint point)
after this the focus will change.

Where I have to place the focus for tabone?
Please be so kind to help.
Best regards,
Thomas



Commented:
((CTabOne*)m_tabPages[m_tabCurrent])->SetInitialFocus();

Try to add this line to initialization code, for example, to the end of CMyTabCtrl::Init function.
P.S.
I also want to highlight each tab.

my variable of the tabcontrol is m_myTabCtrl

on ...Dlg.cpp OnInitDialog()

m_myTabCtrl.InsertItem(0, _T("TabOne"));
m_myTabCtrl.InsertItem(1, _T("TabTwo"));
m_myTabCtrl.InsertItem(2, _T("TabThree"));

m_myTabCtrl.HighlightItem(0);

when the programm starts...the tabOne is on highlight
How can I add this to each tab...when the tab is changed?

Thank you Alex

Commented:
void CMyTabCtrl::OnLButtonDown(UINT nFlags, CPoint point)
{
 CTabCtrl::OnLButtonDown(nFlags, point);

 if(m_tabCurrent != GetCurFocus())
{
  m_tabPages[m_tabCurrent]->ShowWindow(SW_HIDE);
  m_tabCurrent=GetCurFocus();
  m_tabPages[m_tabCurrent]->ShowWindow(SW_SHOW);

  m_myTabCtrl.HighlightItem(m_tabCurrent);

  ...
I tried this before.
Will not find m_myTabCtrl ???
m_myTabCtrl is a variable of the TabControl.
public declared in ..Dlg.h

I have also enclosed header ...Dlg.h will not work.

void CMyTabCtrl::OnLButtonDown(UINT nFlags, CPoint point)
{
 CTabCtrl::OnLButtonDown(nFlags, point);

 if(m_tabCurrent != GetCurFocus())
{
  m_tabPages[m_tabCurrent]->ShowWindow(SW_HIDE);
  m_tabCurrent=GetCurFocus();
  m_tabPages[m_tabCurrent]->ShowWindow(SW_SHOW);

  m_myTabCtrl.HighlightItem(m_tabCurrent);  <----------------------------

  ...
((CTabOne*)m_tabPages[m_tabCurrent])->SetInitialFocus();

Try to add this line to initialization code, for example, to the end of CMyTabCtrl::Init function.


tried this before. Will not work. Only when I was on tabtwo and go back to tabone.
Commented:
Actually, we are inside of this class:

void CMyTabCtrl::OnLButtonDown(UINT nFlags, CPoint point)
{
    ...

    HighlightItem(m_tabCurrent);  

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Yes....your are right that will work.
One silly question. How can I erase the Highlight?

if(m_tabCurrent==0)  HighlightItem(0);  
1 and 2 should not be highlighted

if(m_tabCurrent==1)  HighlightItem(1);  
0 and 2 should not be highlighted

if(m_tabCurrent==2)  HighlightItem(2);  
0 and 1 should not be highlighted

Commented:
for ( int i = 0; i < 3; i++ )
{
    HighlightItem(m_tabCurrent, i == m_tabCurrent);
}
Will not work. Only No. 3 is highlighted after press on tabthree
and then always highlighted

void CMyTabCtrl::OnLButtonDown(UINT nFlags, CPoint point)
{
 CTabCtrl::OnLButtonDown(nFlags, point);

 if(m_tabCurrent != GetCurFocus())
{
  m_tabPages[m_tabCurrent]->ShowWindow(SW_HIDE);
  m_tabCurrent=GetCurFocus();
  m_tabPages[m_tabCurrent]->ShowWindow(SW_SHOW);
  m_tabPages[m_tabCurrent]->SetFocus();


 
  for ( int i = 0; i < 3; i++ )
{
    HighlightItem(m_tabCurrent, i == m_tabCurrent);
}
I found the answer for the Higlight problem:
I have to add the funtion to my ...Dlg.cpp

void CTabControl_Register_mitKlasseDlg::OnTcnSelchangeTab1(NMHDR *pNMHDR, LRESULT *pResult)
{
      
      int nChoice = m_myTabCtrl.GetCurSel();
      
      switch(nChoice)
      {
      case 0:
            m_myTabCtrl.HighlightItem(0,TRUE);
        m_myTabCtrl.HighlightItem(1,FALSE);
            m_myTabCtrl.HighlightItem(2,FALSE);
            break;
      case 1:
        mm_myTabCtrl.HighlightItem(1,TRUE);
        m_myTabCtrl.HighlightItem(0,FALSE);
            m_myTabCtrl.HighlightItem(2,FALSE);
            break;
      case 2:
        m_myTabCtrl.HighlightItem(2,TRUE);
        m_myTabCtrl.HighlightItem(0,FALSE);
            m_myTabCtrl.HighlightItem(1,FALSE);
            break;
      }

      *pResult = 0;
}
//####################################################

Perhaps it is now easy for the focus-problem.
Please let me know Alex, Thanks
Thomas

Hi Alex,
I have just add 500 points with grade A.
Even if we not get everything right. I will ask the question with the
focus problem again.
Thank you.

Best regards,
Thomas
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.