Solved

Need some help with my TabControl = set focus (500 Points Grade A) please help

Posted on 2006-11-24
7
649 Views
Last Modified: 2008-01-09
Hello,

Dialog-Application:
I have a tab-control with 3 tabs.
Each tab has it own class and own dialogbox.
I also have one class CRegisterKontrolle (BaseClass CTabCtrl)

When I start the program tab-one is selected.
The dialogbox of tab-one has two Editboxen.
IDC_EDIT1 and IDC_EDIT2.
When I start the program there is no focus (cursor) in each boxes.

I would like have the focus on IDC_EDIT2 when I start the program.
How can I do this?

Here is my code:
// RegisterKontrolle.cpp : Implementierungsdatei

#include "stdafx.h"
#include "Tab Control_Register_mit Klasse.h"
#include "Tab Control_Register_mit KlasseDlg.h"
#include "RegisterKontrolle.h"
#include ".\registerkontrolle.h"
#include "TabOne.h"
#include "TabTwo.h"
#include "TabThree.h"


IMPLEMENT_DYNAMIC(CRegisterKontrolle, CTabCtrl,)
//----------------- Konstruktor -------------------
CRegisterKontrolle::CRegisterKontrolle()
: m_tabCurrent(0)
, m_nNumberOfPages(0)
{
      m_tabPages[0]=new CTabOne;
      m_tabPages[1]=new CTabTwo;
      m_tabPages[2]=new CTabThree;
      m_nNumberOfPages=3;
}
//---------------- Destruktor ---------------------
CRegisterKontrolle::~CRegisterKontrolle()
{
      for(int nCount=0; nCount<m_nNumberOfPages; nCount++)
      {
            delete m_tabPages[nCount];
      }
}
//-------------------------------------------------
BEGIN_MESSAGE_MAP(CRegisterKontrolle, CTabCtrl)
      ON_WM_LBUTTONDOWN()
      
      ON_NOTIFY(TCN_SELCHANGE, IDC_TAB1, OnTcnSelchangeTab1)
      ON_NOTIFY(NM_CLICK, IDC_TAB1, OnNMClickTab1)
END_MESSAGE_MAP()



// CRegisterKontrolle-Meldungshandler

// ############## Funktion ###############
void CRegisterKontrolle::RegisterAuswahl()
{
      m_tabCurrent=0;
      m_tabPages[0]->Create(IDD_DIALOG1, this);

        m_tabPages[1]->Create(IDD_DIALOG2, this);
        m_tabPages[2]->Create(IDD_DIALOG3, this);

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

      RegisterAufbau();// Funktion aufrufen
      
}
// ############## Funktion ################
void CRegisterKontrolle::RegisterAufbau()
{
      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);
      }
             //--------------------------------------------------
   

}
//################# OnLButtonDown ####################
void CRegisterKontrolle::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();
      }
      
}
//######################################################

// Tab Control_Register_mit KlasseDlg.cpp : Implementierungsdatei
//

#include "stdafx.h"

#include "Tab Control_Register_mit Klasse.h"
#include "Tab Control_Register_mit KlasseDlg.h"
#include "TabOne.h"

#include "RegisterKontrolle.h"
#include ".\tab control_register_mit klassedlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CAboutDlg-Dialogfeld für Anwendungsbefehl 'Info'

class CAboutDlg : public CDialog
{
public:
      CAboutDlg();

// Dialogfelddaten
      enum { IDD = IDD_ABOUTBOX };

      protected:
      virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV-Unterstützung

// Implementierung
protected:
      DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
}

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

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
END_MESSAGE_MAP()


// CTabControl_Register_mitKlasseDlg Dialogfeld



CTabControl_Register_mitKlasseDlg::CTabControl_Register_mitKlasseDlg(CWnd* pParent /*=NULL*/)
      : CDialog(CTabControl_Register_mitKlasseDlg::IDD, pParent)
{
      m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CTabControl_Register_mitKlasseDlg::DoDataExchange(CDataExchange* pDX)
{
      CDialog::DoDataExchange(pDX);
      DDX_Control(pDX, IDC_TAB1, m_myRegister);
}

BEGIN_MESSAGE_MAP(CTabControl_Register_mitKlasseDlg, CDialog)
      ON_WM_SYSCOMMAND()
      ON_WM_PAINT()
      ON_WM_QUERYDRAGICON()
      //}}AFX_MSG_MAP
      ON_NOTIFY(TCN_SELCHANGE, IDC_TAB1, OnTcnSelchangeTab1)
END_MESSAGE_MAP()


// CTabControl_Register_mitKlasseDlg Meldungshandler

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

      // Hinzufügen des Menübefehls "Info..." zum Systemmenü.

      // IDM_ABOUTBOX muss sich im Bereich der Systembefehle befinden.
      ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
      ASSERT(IDM_ABOUTBOX < 0xF000);

      CMenu* pSysMenu = GetSystemMenu(FALSE);
      if (pSysMenu != NULL)
      {
            CString strAboutMenu;
            strAboutMenu.LoadString(IDS_ABOUTBOX);
            if (!strAboutMenu.IsEmpty())
            {
                  pSysMenu->AppendMenu(MF_SEPARATOR);
                  pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
            }
      }

      // Symbol für dieses Dialogfeld festlegen. Wird automatisch erledigt
      //  wenn das Hauptfenster der Anwendung kein Dialogfeld ist
      SetIcon(m_hIcon, TRUE);                  // Großes Symbol verwenden
      SetIcon(m_hIcon, FALSE);            // Kleines Symbol verwenden

      // TODO: Hier zusätzliche Initialisierung einfügen

      //########## Register#########
      m_myRegister.InsertItem(0, _T("Ronden"));
      m_myRegister.InsertItem(1, _T("Tab Two"));
      m_myRegister.InsertItem(2, _T("Tab Three"));

                m_myRegister.RegisterAuswahl();
      m_myRegister.HighlightItem(0);

      //###########################
      return TRUE;  // Geben Sie TRUE zurück, außer ein Steuerelement soll den Fokus erhalten
}

void CTabControl_Register_mitKlasseDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
      if ((nID & 0xFFF0) == IDM_ABOUTBOX)
      {
            CAboutDlg dlgAbout;
            dlgAbout.DoModal();
      }
      else
      {
            CDialog::OnSysCommand(nID, lParam);
      }
}

// Wenn Sie dem Dialogfeld eine Schaltfläche "Minimieren" hinzufügen, benötigen Sie
//  den nachstehenden Code, um das Symbol zu zeichnen. Für MFC-Anwendungen, die das
//  Dokument/Ansicht-Modell verwenden, wird dies automatisch ausgeführt.

void CTabControl_Register_mitKlasseDlg::OnPaint()
{
      if (IsIconic())
      {
            CPaintDC dc(this); // Gerätekontext zum Zeichnen

            SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

            // Symbol in Clientrechteck zentrieren
            int cxIcon = GetSystemMetrics(SM_CXICON);
            int cyIcon = GetSystemMetrics(SM_CYICON);
            CRect rect;
            GetClientRect(&rect);
            int x = (rect.Width() - cxIcon + 1) / 2;
            int y = (rect.Height() - cyIcon + 1) / 2;

            // Symbol zeichnen
            dc.DrawIcon(x, y, m_hIcon);
      }
      else
      {
            CDialog::OnPaint();
      }
}

// Die System ruft diese Funktion auf, um den Cursor abzufragen, der angezeigt wird, während der Benutzer
//  das minimierte Fenster mit der Maus zieht.
HCURSOR CTabControl_Register_mitKlasseDlg::OnQueryDragIcon()
{
      return static_cast<HCURSOR>(m_hIcon);
}






//##################################################
void CTabControl_Register_mitKlasseDlg::OnTcnSelchangeTab1(NMHDR *pNMHDR, LRESULT *pResult)
{
      int nAuswahl = m_myRegister.GetCurSel();

      
      switch(nAuswahl)
      {
      case 0:
               m_myRegister.HighlightItem(0,TRUE);
               m_myRegister.HighlightItem(1,FALSE);
      m_myRegister.HighlightItem(2,FALSE);
        break;
             
      case 1:
               m_myRegister.HighlightItem(1,TRUE);
               m_myRegister.HighlightItem(0,FALSE);
      m_myRegister.HighlightItem(2,FALSE);
      break;

      case 2:
               m_myRegister.HighlightItem(2,TRUE);
               m_myRegister.HighlightItem(0,FALSE);
      m_myRegister.HighlightItem(1,FALSE);
      break;
      }
      

      *pResult = 0;

}
//#####################################################




0
Comment
Question by:tsp2002
  • 4
  • 3
7 Comments
 
LVL 48

Expert Comment

by:AlexFM
ID: 18007251
Look at the changes I did in original CodeGuru sample to set initial focus when application starts.
1. Add two editboxes to first dialog template (IDD_TAB_ONE)
2. Add this function to CMyTabCtrl:

void CMyTabCtrl::SetInitialFocus()
{
    m_tabPages[0]->SetFocus();
}

3. Add line to CMyTabExampleDlg::OnInitDialog:

    m_tabMyTabCtrl.Init();
    SetTimer(100, 20, NULL);    // <- add this line

4. Add WM_TIMER message handler to CMyTabExampleDlg and fill it by this way:

void CMyTabExampleDlg::OnTimer(UINT_PTR nIDEvent)
{
    m_tabMyTabCtrl.SetFocus();
    m_tabMyTabCtrl.SetInitialFocus();
    KillTimer(100);

    CDialog::OnTimer(nIDEvent);
}

Run application. You can see that focus is initially set on the first tab, and first edit box in Tab order gets focus.
If you have some reason to select edit box whis is not first in Tab order, add one more line:

void CMyTabCtrl::SetInitialFocus()
{
    m_tabPages[0]->SetFocus();
    m_tabPages[0]->GotoDlgCtrl(m_tabPages[0]->GetDlgItem(IDC_EDIT2)); // <- select IDC_EDIT2 even is it is not first in Tab order
}
0
 
LVL 48

Accepted Solution

by:
AlexFM earned 500 total points
ID: 18007253
Make the same changes in your program.
0
 

Author Comment

by:tsp2002
ID: 18008404
Hi Alex,
thank you very much for your code.
I will try this on Monday at my office computer.
Will let you know on Monday.
Have a great weekend.
Best regards,
Thomas
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:tsp2002
ID: 18018184
Hi Alex,
wow...it works great!!!!! SUPER...even if I don´t understand all.
I just left you the points with grade A.
Thank you very much for your great help.

Perhaps you can give me some more  information.
--------------------------------------------------------------
1) why do you use : SetTimer ?

2) m_tabPages[0]->GotoDlgCtrl(m_tabPages[0]->GetDlgItem(IDC_EDIT2));
    I never used GotoDlgCtrl ?

3) ((CTabTwo*)m_tabPages[m_tabCurrent])->SetInitialFocus();
     is this kind of a way to get access to another class element?
     even if this class is not deduced/deflected or inherited from the other?

I have to learn a lot....


0
 
LVL 48

Expert Comment

by:AlexFM
ID: 18018318
1. SetInitialFocus function called directly from OnInitDialog doesn't work, because standard dialog initialization overwrites SetInitialFocus results. I want to call SetInitialFocus AFTER standard dialog initialization. One timer tick does this - SetInitialFocus is called asynchrolously.

2. GotoDlgCtrl sets focus to dialog control.

3. This is casting - m_tabPages has CDialog type, and I want to cast it to CTabOne, Two ... to call SetInitialFocus function.
0
 

Author Comment

by:tsp2002
ID: 18019166
Okay....thanks for the information.
I very appreciate your help.
Keep up the good work at ExpertsExchange.com
Best regards,
Thomas
0
 

Author Comment

by:tsp2002
ID: 18019183
P.S. Do you have an answer for this guestion?
You tried something, but I think that will not work, I had to ingnore that.
Please let me know, thanks Alex
How can I add a Icon to my TabControl ??? 500 Points
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

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…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

910 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

25 Experts available now in Live!

Get 1:1 Help Now