Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2006-11-21
19
Medium Priority
?
243 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 11
  • 8
19 Comments
 
LVL 48

Expert Comment

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

Author Comment

by:tsp2002
ID: 17987665
How can I do this?
0
 
LVL 48

Expert Comment

by:AlexFM
ID: 17988105
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
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.

 

Author Comment

by:tsp2002
ID: 17988524
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
ID: 17989314
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
ID: 17995753
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
ID: 17995954
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
ID: 18001816
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
ID: 18001955
((CTabOne*)m_tabPages[m_tabCurrent])->SetInitialFocus();

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

Author Comment

by:tsp2002
ID: 18001962
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
ID: 18002085
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
ID: 18002480
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
ID: 18002504
((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 2000 total points
ID: 18002582
Actually, we are inside of this class:

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

    HighlightItem(m_tabCurrent);  
0
 

Author Comment

by:tsp2002
ID: 18002687
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
ID: 18002735
for ( int i = 0; i < 3; i++ )
{
    HighlightItem(m_tabCurrent, i == m_tabCurrent);
}
0
 

Author Comment

by:tsp2002
ID: 18002781
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
ID: 18003124
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
ID: 18006633
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

664 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