MFC: Easier way to know which Edit Control has the focus?
-
Hi, In my program, I have 56 Edit Controls where I can type the names of 56 players. Presently, I'm using ON_EN_SETFOCUS for every Edit Control to set an INT variable that tells me which Edit Control has the focus. When I want to print, delete or view the result of the player in focus, I check the variable and than print or delete the good player. Is there an easier way to know the ID of the Edit Control in focus? Like when I want to print a player's results, just get the Edit Control ID in focus at this moment, compare the ID and identify the player. Here's a sample of my code: .... ON_EN_SETFOCUS(IDC_NAME13, OnEnSetfocusName13) ON_EN_SETFOCUS(IDC_NAME14, OnEnSetfocusName14) END_MESSAGE_MAP() void CMainView::OnEnSetfocusName14() { PlayerInFocus = 14; } What I would like to do: void CMainView::OnPrintPlayer() { Get the control ID in focus if(Control ID == 14) Print Player 14 else if(Control ID == 15) Print Player 15 else if ... } Thanks Dave
-
Hi, In my program, I have 56 Edit Controls where I can type the names of 56 players. Presently, I'm using ON_EN_SETFOCUS for every Edit Control to set an INT variable that tells me which Edit Control has the focus. When I want to print, delete or view the result of the player in focus, I check the variable and than print or delete the good player. Is there an easier way to know the ID of the Edit Control in focus? Like when I want to print a player's results, just get the Edit Control ID in focus at this moment, compare the ID and identify the player. Here's a sample of my code: .... ON_EN_SETFOCUS(IDC_NAME13, OnEnSetfocusName13) ON_EN_SETFOCUS(IDC_NAME14, OnEnSetfocusName14) END_MESSAGE_MAP() void CMainView::OnEnSetfocusName14() { PlayerInFocus = 14; } What I would like to do: void CMainView::OnPrintPlayer() { Get the control ID in focus if(Control ID == 14) Print Player 14 else if(Control ID == 15) Print Player 15 else if ... } Thanks Dave
just make sure your resource ID`s are in consecutive eg IDC_NAME1=21 IDC_NAME2=22 . . . IDC_NAME26=46 and then you could use in messagemap: ON_NOTIFY_RANGE( NM_SETFOCUS, IDC_NAME1, IDC_NAME26, SetPlayerFocus ) define SetPlayerFocus in header as: afx_msg void SetPlayerFocus(UINT nid, NMHDR* pNotifyStruct, LRESULT* pResult); void CMainView::SetPlayerFocus(UINT nid, NMHDR* pNotifyStruct, LRESULT* pResult) { m_nPlayerWithFocus = nid - IDC_NAME1 +1 ; //results in 1 for player1 etc... pResult = 0; } void CMainView::OnPrintPlayer() { // print whatever you like, m_nPlayerWithFocus contains value of last edit field that had focus } I think that should roughly do the trick... . . . . other option, maybe even better skip the setfocus function just do: void CMainView::OnPrintPlayer() { CWnd * wndFoc = GetFocus(); if( !wndFoc ) return; int nCurrentPlayer = wndFoc->GetDlgCtrlID() - IDC_NAME1 +1; if( 0=nCurrentPlayer ) PrintResultsFor(nCurrentPlayer); }
-
Hi, In my program, I have 56 Edit Controls where I can type the names of 56 players. Presently, I'm using ON_EN_SETFOCUS for every Edit Control to set an INT variable that tells me which Edit Control has the focus. When I want to print, delete or view the result of the player in focus, I check the variable and than print or delete the good player. Is there an easier way to know the ID of the Edit Control in focus? Like when I want to print a player's results, just get the Edit Control ID in focus at this moment, compare the ID and identify the player. Here's a sample of my code: .... ON_EN_SETFOCUS(IDC_NAME13, OnEnSetfocusName13) ON_EN_SETFOCUS(IDC_NAME14, OnEnSetfocusName14) END_MESSAGE_MAP() void CMainView::OnEnSetfocusName14() { PlayerInFocus = 14; } What I would like to do: void CMainView::OnPrintPlayer() { Get the control ID in focus if(Control ID == 14) Print Player 14 else if(Control ID == 15) Print Player 15 else if ... } Thanks Dave
As a note, you could use: CWnd *pFocus = GetFocus(); if (pFocus && pFocus->IsKindOf(RUNTIME_CLASS(CEdit))) { UINT id = pFocus->GetDlgCtrlID(); // makse use of the controls id here.... Roger Allen - Sonork 100.10016 Roger Wright: Remember to buckle up, please, and encourage your friends to do the same. It's not just about saving your life, but saving the quality of life for those you may leave behind...
-
Hi, In my program, I have 56 Edit Controls where I can type the names of 56 players. Presently, I'm using ON_EN_SETFOCUS for every Edit Control to set an INT variable that tells me which Edit Control has the focus. When I want to print, delete or view the result of the player in focus, I check the variable and than print or delete the good player. Is there an easier way to know the ID of the Edit Control in focus? Like when I want to print a player's results, just get the Edit Control ID in focus at this moment, compare the ID and identify the player. Here's a sample of my code: .... ON_EN_SETFOCUS(IDC_NAME13, OnEnSetfocusName13) ON_EN_SETFOCUS(IDC_NAME14, OnEnSetfocusName14) END_MESSAGE_MAP() void CMainView::OnEnSetfocusName14() { PlayerInFocus = 14; } What I would like to do: void CMainView::OnPrintPlayer() { Get the control ID in focus if(Control ID == 14) Print Player 14 else if(Control ID == 15) Print Player 15 else if ... } Thanks Dave
Maybe I'm missing something, but why can't you just
GetFocus()
? This will return aCWnd*
of the window having the focus.
"The pointy end goes in the other man." - Antonio Banderas (Zorro, 1998)
-
As a note, you could use: CWnd *pFocus = GetFocus(); if (pFocus && pFocus->IsKindOf(RUNTIME_CLASS(CEdit))) { UINT id = pFocus->GetDlgCtrlID(); // makse use of the controls id here.... Roger Allen - Sonork 100.10016 Roger Wright: Remember to buckle up, please, and encourage your friends to do the same. It's not just about saving your life, but saving the quality of life for those you may leave behind...