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. How to get datas from an edit control (in the toolbar) by pressing return (enter) ?

How to get datas from an edit control (in the toolbar) by pressing return (enter) ?

Scheduled Pinned Locked Moved C / C++ / MFC
helpphptutorialquestion
24 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.
  • C CrocodileBuck

    Ok, i'put this code in the newToolBar.cpp: BOOL New_cToolBar::PreTranslateMessage(MSG* pMsg) { if (pMsg->message==WM_KEYDOWN && pMsg->wParam == VK_RETURN ) { if (pMsg->message==WM_KEYDOWN && pMsg->wParam == VK_RETURN ) { if ( ID_nEDIT ) { MessageBox( _T("Got it!") ); } } } return New_cToolBar::PreTranslateMessage( pMsg ); } but int won't work ??? best regards Croc

    M Offline
    M Offline
    Mark Salsbery
    wrote on last edited by
    #9

    That won't work because the message goes to the edit control, not to the toolbar :) Mark

    Mark Salsbery Microsoft MVP - Visual C++ :java:

    C 1 Reply Last reply
    0
    • M Mark Salsbery

      That won't work because the message goes to the edit control, not to the toolbar :) Mark

      Mark Salsbery Microsoft MVP - Visual C++ :java:

      C Offline
      C Offline
      CrocodileBuck
      wrote on last edited by
      #10

      Thx Mr. Salsbery, are there any CodeSnippets out there ? I think i couldn't manage it without ... :sigh: Best regards Croc

      C M 2 Replies Last reply
      0
      • C CrocodileBuck

        Thx Mr. Salsbery, are there any CodeSnippets out there ? I think i couldn't manage it without ... :sigh: Best regards Croc

        C Offline
        C Offline
        CrocodileBuck
        wrote on last edited by
        #11

        Perhaps you could have a look on my Code i posted !? Thx Best regards croc

        1 Reply Last reply
        0
        • C CrocodileBuck

          Thx Mr. Salsbery, are there any CodeSnippets out there ? I think i couldn't manage it without ... :sigh: Best regards Croc

          M Offline
          M Offline
          Mark Salsbery
          wrote on last edited by
          #12

          // MyEdit.h

          #pragma once

          class CMyEdit : public CEdit
          {
              DECLARE_DYNAMIC(CMyEdit)

          public:
              CMyEdit();
              virtual ~CMyEdit();

          protected:
              DECLARE_MESSAGE_MAP()
              afx_msg void OnChar(UINT nChar, UINT nRepCnt, UINT nFlags);
          };

          ////////////////////////////////////////////////////////////////////////

          // MyEdit.cpp

          #include "stdafx.h"
          #include "MyEdit.h"

          IMPLEMENT_DYNAMIC(CMyEdit, CEdit)

          CMyEdit::CMyEdit() : CEdit()
          {
          }

          CMyEdit::~CMyEdit()
          {
          }

          BEGIN_MESSAGE_MAP(CMyEdit, CEdit)
              ON_WM_CHAR()
          END_MESSAGE_MAP()

          void CMyEdit::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
          {
              if (nChar == VK_RETURN)
              {
                  // do stuff!

          return;
              }

          CEdit::OnChar(nChar, nRepCnt, nFlags);
          }

          Mark Salsbery Microsoft MVP - Visual C++ :java:

          C 1 Reply Last reply
          0
          • M Mark Salsbery

            // MyEdit.h

            #pragma once

            class CMyEdit : public CEdit
            {
                DECLARE_DYNAMIC(CMyEdit)

            public:
                CMyEdit();
                virtual ~CMyEdit();

            protected:
                DECLARE_MESSAGE_MAP()
                afx_msg void OnChar(UINT nChar, UINT nRepCnt, UINT nFlags);
            };

            ////////////////////////////////////////////////////////////////////////

            // MyEdit.cpp

            #include "stdafx.h"
            #include "MyEdit.h"

            IMPLEMENT_DYNAMIC(CMyEdit, CEdit)

            CMyEdit::CMyEdit() : CEdit()
            {
            }

            CMyEdit::~CMyEdit()
            {
            }

            BEGIN_MESSAGE_MAP(CMyEdit, CEdit)
                ON_WM_CHAR()
            END_MESSAGE_MAP()

            void CMyEdit::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
            {
                if (nChar == VK_RETURN)
                {
                    // do stuff!

            return;
                }

            CEdit::OnChar(nChar, nRepCnt, nFlags);
            }

            Mark Salsbery Microsoft MVP - Visual C++ :java:

            C Offline
            C Offline
            CrocodileBuck
            wrote on last edited by
            #13

            Many thx Mr. Salsbery, this will work, but only in a seperate project! Now i try to put your code in my code with the edit in the toolbar ! Where and how have i to implement it ???:confused: I never did anything in the MFC that was as complicated like this ! Many Thanx Croc

            M 1 Reply Last reply
            0
            • C CrocodileBuck

              Many thx Mr. Salsbery, this will work, but only in a seperate project! Now i try to put your code in my code with the edit in the toolbar ! Where and how have i to implement it ???:confused: I never did anything in the MFC that was as complicated like this ! Many Thanx Croc

              M Offline
              M Offline
              Mark Salsbery
              wrote on last edited by
              #14

              CrocodileBuck wrote:

              this will work, but only in a seperate project!

              You can't add another class to your project? :) How did you get the edit control on the toolbar (the code)?

              Mark Salsbery Microsoft MVP - Visual C++ :java:

              C 1 Reply Last reply
              0
              • M Mark Salsbery

                CrocodileBuck wrote:

                this will work, but only in a seperate project!

                You can't add another class to your project? :) How did you get the edit control on the toolbar (the code)?

                Mark Salsbery Microsoft MVP - Visual C++ :java:

                C Offline
                C Offline
                CrocodileBuck
                wrote on last edited by
                #15

                ??? Shur i can add classes but i'm ....:confused: ! I don't know where i have to implement your code and i don't know how to do it ! I tried it too long i think ! :( This is the declaration of the membervariable of my edit in NewToolBar.h : public: //************************************************************************************************************ CEdit m_Ctrl_EDIT; //************************************************************************************************************ In NewToolBar.cpp: BOOL NewToolBar::LoadToolBarEx(UINT id) { BOOL bReturn; bReturn = CToolBar::LoadToolBar(id); // Zugriff auf Elemente der Funktion LoadToolbar() int iPosition = CommandToIndex(ID_nEDIT); SetButtonInfo(iPosition,ID_nEDIT,TBBS_SEPARATOR,EDITLEN); CRect rect; GetItemRect(iPosition, rect); rect.bottom = 20; DWORD dwStyle = WS_CHILD|WS_VISIBLE|WS_TABSTOP|ES_AUTOHSCROLL;/*ES_NUMBER|*/ m_Ctrl_EDIT.CreateEx(WS_EX_CLIENTEDGE,_T("EDIT"),NULL, dwStyle, rect, this, ID_nEDIT); return bReturn; } i use the menber !!! i don't know how to implement code into mine, this is the prob ! Could you perhaps show it on my sample i posted, i know it's not the general way, im not to lazy, it simply won'work :( :( :( :sigh: :sigh: :sigh: :(( :(( :(( Thanx a lot Best regards Croc(master of desaster)

                M 1 Reply Last reply
                0
                • C CrocodileBuck

                  ??? Shur i can add classes but i'm ....:confused: ! I don't know where i have to implement your code and i don't know how to do it ! I tried it too long i think ! :( This is the declaration of the membervariable of my edit in NewToolBar.h : public: //************************************************************************************************************ CEdit m_Ctrl_EDIT; //************************************************************************************************************ In NewToolBar.cpp: BOOL NewToolBar::LoadToolBarEx(UINT id) { BOOL bReturn; bReturn = CToolBar::LoadToolBar(id); // Zugriff auf Elemente der Funktion LoadToolbar() int iPosition = CommandToIndex(ID_nEDIT); SetButtonInfo(iPosition,ID_nEDIT,TBBS_SEPARATOR,EDITLEN); CRect rect; GetItemRect(iPosition, rect); rect.bottom = 20; DWORD dwStyle = WS_CHILD|WS_VISIBLE|WS_TABSTOP|ES_AUTOHSCROLL;/*ES_NUMBER|*/ m_Ctrl_EDIT.CreateEx(WS_EX_CLIENTEDGE,_T("EDIT"),NULL, dwStyle, rect, this, ID_nEDIT); return bReturn; } i use the menber !!! i don't know how to implement code into mine, this is the prob ! Could you perhaps show it on my sample i posted, i know it's not the general way, im not to lazy, it simply won'work :( :( :( :sigh: :sigh: :sigh: :(( :(( :(( Thanx a lot Best regards Croc(master of desaster)

                  M Offline
                  M Offline
                  Mark Salsbery
                  wrote on last edited by
                  #16

                  CrocodileBuck wrote:

                  CEdit m_Ctrl_EDIT;

                  Change that line to CMyEdit m_Ctrl_EDIT; I gave you code that can be separated into a .h and a .cpp file. Do that, and add the files to your project.  :) Add the following line above the NewToolBar class decalration: #include "MyEdit.h" That's it!

                  CrocodileBuck wrote:

                  Could you perhaps show it on my sample i posted

                  I could, but you wouldn't learn anything.  You should (IMO) be able to easily add classes to your projects. You'll be happier in the long run I bet :) Mark

                  Mark Salsbery Microsoft MVP - Visual C++ :java:

                  C 1 Reply Last reply
                  0
                  • M Mark Salsbery

                    CrocodileBuck wrote:

                    CEdit m_Ctrl_EDIT;

                    Change that line to CMyEdit m_Ctrl_EDIT; I gave you code that can be separated into a .h and a .cpp file. Do that, and add the files to your project.  :) Add the following line above the NewToolBar class decalration: #include "MyEdit.h" That's it!

                    CrocodileBuck wrote:

                    Could you perhaps show it on my sample i posted

                    I could, but you wouldn't learn anything.  You should (IMO) be able to easily add classes to your projects. You'll be happier in the long run I bet :) Mark

                    Mark Salsbery Microsoft MVP - Visual C++ :java:

                    C Offline
                    C Offline
                    CrocodileBuck
                    wrote on last edited by
                    #17

                    Hi Mr.Salsbery, yap i'm sure you are right, the long way is better, and i'm able to add classes to my project :sigh: I added your code bur it still won't work ! :( http://www.filehosting.at/files/download.php?file=422e731948daac3a203df0de8c83b21b Thax a lot Best regards Croc :sigh:

                    C 1 Reply Last reply
                    0
                    • C CrocodileBuck

                      Hi Mr.Salsbery, yap i'm sure you are right, the long way is better, and i'm able to add classes to my project :sigh: I added your code bur it still won't work ! :( http://www.filehosting.at/files/download.php?file=422e731948daac3a203df0de8c83b21b Thax a lot Best regards Croc :sigh:

                      C Offline
                      C Offline
                      CrocodileBuck
                      wrote on last edited by
                      #18

                      Hi, i don't get an error message ! It simpy dont work ;) :^) :sigh: Best regards Croc

                      M 1 Reply Last reply
                      0
                      • C CrocodileBuck

                        Hi, i don't get an error message ! It simpy dont work ;) :^) :sigh: Best regards Croc

                        M Offline
                        M Offline
                        Mark Salsbery
                        wrote on last edited by
                        #19

                        If you put a breakpoint in OnChar() and run it in the debugger, is it ever hit when you type in the control? If not, put a breakpoint in the CMyEdit constructor.  Is that ever hit?  If not then you didn't use a CMyEdit instead of a CEdit. I'd look at your code if it was in a ZIP - I have no unrar utility. Mark

                        Mark Salsbery Microsoft MVP - Visual C++ :java:

                        C 1 Reply Last reply
                        0
                        • M Mark Salsbery

                          If you put a breakpoint in OnChar() and run it in the debugger, is it ever hit when you type in the control? If not, put a breakpoint in the CMyEdit constructor.  Is that ever hit?  If not then you didn't use a CMyEdit instead of a CEdit. I'd look at your code if it was in a ZIP - I have no unrar utility. Mark

                          Mark Salsbery Microsoft MVP - Visual C++ :java:

                          C Offline
                          C Offline
                          CrocodileBuck
                          wrote on last edited by
                          #20

                          Hi Mr.Salsbery, no problem, here is the zip: http://www.filehosting.at/files/download.php?file=28f8239afa7e8afc1de1773edd074a02 best regards Croc

                          M 2 Replies Last reply
                          0
                          • C CrocodileBuck

                            Hi Mr.Salsbery, no problem, here is the zip: http://www.filehosting.at/files/download.php?file=28f8239afa7e8afc1de1773edd074a02 best regards Croc

                            M Offline
                            M Offline
                            Mark Salsbery
                            wrote on last edited by
                            #21

                            I'll take a look.... In the meantime, see if adding this to the CMyEdit class works: Add to CMyEdit.h, in the CMyEdit class declaration:

                            public:
                                virtual BOOL PreTranslateMessage(MSG* pMsg);

                            Add to CMyEdit.cpp

                            BOOL CTestEdit::PreTranslateMessage(MSG* pMsg)
                            {
                                if (pMsg->message == WM_KEYDOWN && VK_RETURN == pMsg->wParam)
                                {
                                    return TRUE;
                                }
                                else if (pMsg->message == WM_KEYUP && VK_RETURN == pMsg->wParam)
                                {
                                    // Do stuff!

                            return TRUE;
                                }

                            return CEdit::PreTranslateMessage(pMsg);
                            }

                            Maybe that will work better! :) Mark

                            Mark Salsbery Microsoft MVP - Visual C++ :java:

                            L 1 Reply Last reply
                            0
                            • M Mark Salsbery

                              I'll take a look.... In the meantime, see if adding this to the CMyEdit class works: Add to CMyEdit.h, in the CMyEdit class declaration:

                              public:
                                  virtual BOOL PreTranslateMessage(MSG* pMsg);

                              Add to CMyEdit.cpp

                              BOOL CTestEdit::PreTranslateMessage(MSG* pMsg)
                              {
                                  if (pMsg->message == WM_KEYDOWN && VK_RETURN == pMsg->wParam)
                                  {
                                      return TRUE;
                                  }
                                  else if (pMsg->message == WM_KEYUP && VK_RETURN == pMsg->wParam)
                                  {
                                      // Do stuff!

                              return TRUE;
                                  }

                              return CEdit::PreTranslateMessage(pMsg);
                              }

                              Maybe that will work better! :) Mark

                              Mark Salsbery Microsoft MVP - Visual C++ :java:

                              L Offline
                              L Offline
                              led mike
                              wrote on last edited by
                              #22

                              Wow 20 some messages to get text from an edit control? WOW :omg: I can't get myself to read this thread. Be afraid, very afraid. :-D

                              led mike

                              1 Reply Last reply
                              0
                              • C CrocodileBuck

                                Hi Mr.Salsbery, no problem, here is the zip: http://www.filehosting.at/files/download.php?file=28f8239afa7e8afc1de1773edd074a02 best regards Croc

                                M Offline
                                M Offline
                                Mark Salsbery
                                wrote on last edited by
                                #23

                                Ok, I steered you wrong with the enter key, sorry.  Change your CMyEdit class like this and it works fine in your code:

                                // MyEdit.h

                                #pragma once

                                class CMyEdit : public CEdit
                                {
                                    DECLARE_DYNAMIC(CMyEdit)
                                       
                                public:
                                    CMyEdit();
                                    virtual ~CMyEdit();

                                virtual BOOL PreTranslateMessage(MSG* pMsg);
                                   
                                protected:
                                    DECLARE_MESSAGE_MAP()
                                };

                                ///////////////////////////////////////////////////////////////////////////

                                // MyEdit.cpp

                                #include "stdafx.h"
                                #include "MyEdit.h"

                                IMPLEMENT_DYNAMIC(CMyEdit, CEdit)

                                CMyEdit::CMyEdit() : CEdit()
                                {
                                }

                                CMyEdit::~CMyEdit()
                                {
                                }

                                BEGIN_MESSAGE_MAP(CMyEdit, CEdit)
                                END_MESSAGE_MAP()

                                BOOL CMyEdit::PreTranslateMessage(MSG* pMsg)
                                {
                                    if (pMsg->message == WM_KEYDOWN && VK_RETURN == pMsg->wParam)
                                    {
                                        return TRUE;
                                    }
                                    else if (pMsg->message == WM_KEYUP && VK_RETURN == pMsg->wParam)
                                    {
                                        // Do stuff!

                                MessageBox("Works","Works" ,MB_OK);

                                return TRUE;
                                    }

                                return CEdit::PreTranslateMessage(pMsg);
                                }

                                Mark

                                Mark Salsbery Microsoft MVP - Visual C++ :java:

                                C 1 Reply Last reply
                                0
                                • M Mark Salsbery

                                  Ok, I steered you wrong with the enter key, sorry.  Change your CMyEdit class like this and it works fine in your code:

                                  // MyEdit.h

                                  #pragma once

                                  class CMyEdit : public CEdit
                                  {
                                      DECLARE_DYNAMIC(CMyEdit)
                                         
                                  public:
                                      CMyEdit();
                                      virtual ~CMyEdit();

                                  virtual BOOL PreTranslateMessage(MSG* pMsg);
                                     
                                  protected:
                                      DECLARE_MESSAGE_MAP()
                                  };

                                  ///////////////////////////////////////////////////////////////////////////

                                  // MyEdit.cpp

                                  #include "stdafx.h"
                                  #include "MyEdit.h"

                                  IMPLEMENT_DYNAMIC(CMyEdit, CEdit)

                                  CMyEdit::CMyEdit() : CEdit()
                                  {
                                  }

                                  CMyEdit::~CMyEdit()
                                  {
                                  }

                                  BEGIN_MESSAGE_MAP(CMyEdit, CEdit)
                                  END_MESSAGE_MAP()

                                  BOOL CMyEdit::PreTranslateMessage(MSG* pMsg)
                                  {
                                      if (pMsg->message == WM_KEYDOWN && VK_RETURN == pMsg->wParam)
                                      {
                                          return TRUE;
                                      }
                                      else if (pMsg->message == WM_KEYUP && VK_RETURN == pMsg->wParam)
                                      {
                                          // Do stuff!

                                  MessageBox("Works","Works" ,MB_OK);

                                  return TRUE;
                                      }

                                  return CEdit::PreTranslateMessage(pMsg);
                                  }

                                  Mark

                                  Mark Salsbery Microsoft MVP - Visual C++ :java:

                                  C Offline
                                  C Offline
                                  CrocodileBuck
                                  wrote on last edited by
                                  #24

                                  Thanx Mr. Salsbery, many, many Thanx ! no i will go to bed ;) :laugh: :laugh: :-D Perhabs tomorrow i have some questions about the code, but meanwhile 10000000 Thanx ;)) Croc

                                  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