Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 245
  • Last Modified:

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

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
}

0
tsp2002
Asked:
tsp2002
  • 11
  • 8
1 Solution
 
AlexFMCommented:
Maybe it is better to set Tab Order in the dialog template?
0
 
tsp2002Author Commented:
How can I do this?
0
 
AlexFMCommented:
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.
0
New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

 
tsp2002Author Commented:
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(); <-----------------------------!!



0
 
AlexFMCommented:
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));
}
0
 
tsp2002Author Commented:
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

0
 
AlexFMCommented:
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.
0
 
tsp2002Author Commented:
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



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

Try to add this line to initialization code, for example, to the end of CMyTabCtrl::Init function.
0
 
tsp2002Author Commented:
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

0
 
AlexFMCommented:
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);

  ...
0
 
tsp2002Author Commented:
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);  <----------------------------

  ...
0
 
tsp2002Author Commented:
((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.
0
 
AlexFMCommented:
Actually, we are inside of this class:

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

    HighlightItem(m_tabCurrent);  
0
 
tsp2002Author Commented:
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
0
 
AlexFMCommented:
for ( int i = 0; i < 3; i++ )
{
    HighlightItem(m_tabCurrent, i == m_tabCurrent);
}
0
 
tsp2002Author Commented:
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);
}
0
 
tsp2002Author Commented:
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

0
 
tsp2002Author Commented:
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
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

  • 11
  • 8
Tackle projects and never again get stuck behind a technical roadblock.
Join Now