Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. MFC: Easier way to know which Edit Control has the focus?

MFC: Easier way to know which Edit Control has the focus?

Scheduled Pinned Locked Moved C / C++ / MFC
c++question
5 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    Davex_
    wrote on last edited by
    #1

    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

    G R D 3 Replies Last reply
    0
    • D Davex_

      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

      G Offline
      G Offline
      GDavy
      wrote on last edited by
      #2

      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); }

      1 Reply Last reply
      0
      • D Davex_

        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

        R Offline
        R Offline
        Roger Allen
        wrote on last edited by
        #3

        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...

        D 1 Reply Last reply
        0
        • D Davex_

          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

          D Offline
          D Offline
          David Crow
          wrote on last edited by
          #4

          Maybe I'm missing something, but why can't you just GetFocus()? This will return a CWnd* of the window having the focus.


          "The pointy end goes in the other man." - Antonio Banderas (Zorro, 1998)

          1 Reply Last reply
          0
          • R Roger Allen

            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...

            D Offline
            D Offline
            Davex_
            wrote on last edited by
            #5

            Thanks a lot, that's exactly what I was looking for! I'm a beginner, so I'm learning... :-) Dave

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups