Solved

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

Posted on 2006-11-21
19
229 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
}

0
Comment
Question by:tsp2002
  • 11
  • 8
19 Comments
 
LVL 48

Expert Comment

by:AlexFM
Comment Utility
Maybe it is better to set Tab Order in the dialog template?
0
 

Author Comment

by:tsp2002
Comment Utility
How can I do this?
0
 
LVL 48

Expert Comment

by:AlexFM
Comment Utility
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
 

Author Comment

by:tsp2002
Comment Utility
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
 
LVL 48

Expert Comment

by:AlexFM
Comment Utility
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
 

Author Comment

by:tsp2002
Comment Utility
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
 
LVL 48

Expert Comment

by:AlexFM
Comment Utility
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
 

Author Comment

by:tsp2002
Comment Utility
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
 
LVL 48

Expert Comment

by:AlexFM
Comment Utility
((CTabOne*)m_tabPages[m_tabCurrent])->SetInitialFocus();

Try to add this line to initialization code, for example, to the end of CMyTabCtrl::Init function.
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 

Author Comment

by:tsp2002
Comment Utility
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
 
LVL 48

Expert Comment

by:AlexFM
Comment Utility
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
 

Author Comment

by:tsp2002
Comment Utility
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
 

Author Comment

by:tsp2002
Comment Utility
((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
 
LVL 48

Accepted Solution

by:
AlexFM earned 500 total points
Comment Utility
Actually, we are inside of this class:

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

    HighlightItem(m_tabCurrent);  
0
 

Author Comment

by:tsp2002
Comment Utility
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
 
LVL 48

Expert Comment

by:AlexFM
Comment Utility
for ( int i = 0; i < 3; i++ )
{
    HighlightItem(m_tabCurrent, i == m_tabCurrent);
}
0
 

Author Comment

by:tsp2002
Comment Utility
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
 

Author Comment

by:tsp2002
Comment Utility
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
 

Author Comment

by:tsp2002
Comment Utility
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 your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

The following diagram presents a diamond class hierarchy: As depicted, diamond inheritance denotes when two classes (e.g., CDerived1 and CDerived2), separately extending a common base class (e.g., CBase), are sub classed simultaneously by a fourt…
In Easy String Encryption Using CryptoAPI in C++ (http://www.experts-exchange.com/viewArticle.jsp?aid=1193) I described how to encrypt text and recommended that the encrypted text be stored as a series of hexadecimal digits -- because cyphertext may…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now