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

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;

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




Thomas StockbrueggerAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
AlexFMConnect With a Mentor Commented:
Make the same changes in your program.
0
 
AlexFMCommented:
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
 
Thomas StockbrueggerAuthor Commented:
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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
Thomas StockbrueggerAuthor Commented:
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
 
AlexFMCommented:
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
 
Thomas StockbrueggerAuthor Commented:
Okay....thanks for the information.
I very appreciate your help.
Keep up the good work at ExpertsExchange.com
Best regards,
Thomas
0
 
Thomas StockbrueggerAuthor Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.