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 return value after user input

how to return value after user input

Scheduled Pinned Locked Moved C / C++ / MFC
c++comhelptutorialquestion
27 Posts 4 Posters 37 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.
  • M Member 2119844

    Hi All, i need to return input value after press enter. how to make it. need little help. the main think here that i have to make function that return value after press Enter. here is link of project file. https://skydrive.live.com/redir?resid=78B222720843DE16!116&authkey=!AAjKOKZHSd-q9lI i have a dialog box with 2 EditBox (m_TxInput and m_TxOutPut) and 2 Button for (GetString and GetNumber) i made class CMyTextBox for m_TxInput . here is code below. i have 2 function GetString and GetNumber in CMyTextBox Class. i am calling these function on button down in dlg.

    /////////////////////////////////////////////////////////////////////////////
    // CMyTextBox window

    class CMyTextBox : public CEdit
    {
    // Construction
    public:
    CMyTextBox();

    // Attributes
    public:

    // Operations
    public:

    bool InFlag; // After Press Enter InFlag = true;
    CString RetVal; // store return value
    

    // Overrides
    // ClassWizard generated virtual function overrides
    //{{AFX_VIRTUAL(CMyTextBox)
    //}}AFX_VIRTUAL

    // Implementation
    public:
    CString GetString();
    double GetNumber();
    virtual ~CMyTextBox();

    // Generated message map functions
    

    protected:
    //{{AFX_MSG(CMyTextBox)
    afx_msg void OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags);
    //}}AFX_MSG

    DECLARE\_MESSAGE\_MAP()
    

    };

    // CPP file======================================================

    // CMyTextBox message handlers

    void CMyTextBox::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
    {
    // TODO: Add your message handler code here and/or call default
    if (nFlags == 28) // Enter press
    GetWindowText(RetVal);
    CEdit::OnKeyDown(nChar, nRepCnt, nFlags);
    }

    double CMyTextBox::GetNumber()
    {
    // how to get return here after press enter by user
    if (InFlag == true)
    return atof(RetVal);
    return 0;
    }

    CString CMyTextBox::GetString()
    {
    // how to get return here after press enter by user
    if (InFlag == true)
    return RetVal;
    return "";
    }

    Thanks Amrit

    S Offline
    S Offline
    Santhosh G_
    wrote on last edited by
    #3

    I felt three problems. 1) InFlag is always false. You have to set it to true from OnKeyDown with Enter key.

    if (nFlags == 28) // Enter press
    {
    InFlag = true;
    GetWindowText(RetVal);
    }

    1. Please ensure you added ON_WM_KEYDOWN() in message map of CMyTextBox class. 3) Normally an Edit control will not get Enter key press, On Enter keypress, focused button click action will be performed. One option is to change the style of your Edit control to Multi-line. Change MultiLine to true, or enable ES_MULTILINE for the CMyTextBox window style. Another option is to identify enter keypress from PreTranslateMessage(), if "VK_ENTER" key down is occurred from edit control set your InFlag to true.
    M 1 Reply Last reply
    0
    • S Santhosh G_

      I felt three problems. 1) InFlag is always false. You have to set it to true from OnKeyDown with Enter key.

      if (nFlags == 28) // Enter press
      {
      InFlag = true;
      GetWindowText(RetVal);
      }

      1. Please ensure you added ON_WM_KEYDOWN() in message map of CMyTextBox class. 3) Normally an Edit control will not get Enter key press, On Enter keypress, focused button click action will be performed. One option is to change the style of your Edit control to Multi-line. Change MultiLine to true, or enable ES_MULTILINE for the CMyTextBox window style. Another option is to identify enter keypress from PreTranslateMessage(), if "VK_ENTER" key down is occurred from edit control set your InFlag to true.
      M Offline
      M Offline
      Member 2119844
      wrote on last edited by
      #4

      yes i have add InFlag = true; it is not working.

      CString CMyTextBox::GetString()
      {
      // how to come here after press enter by user that is main thing
      if (InFlag == true)
      return RetVal;
      return "";
      }

      it is call form dialog class here is code.

      void CRetValTestDlg::OnGetString()
      {
      m_TxInput.InFlag = false;
      CString s = m_TxInput.GetString();
      m_TxOutput.SetWindowText (s);
      }
      void CRetValTestDlg::OnGetNumber()
      {
      m_TxInput.InFlag = false;
      CString s;
      double d = m_TxInput.GetNumber();
      s.Format ("%d",d);
      m_TxOutput.SetWindowText (s);
      }

      i would like to upload project file for review. if possible.

      S 1 Reply Last reply
      0
      • L Lost User

        This is not the best way to implement a dialog. You should allow the user to fill in the various fields and then capture the data when the OK button is pressed. In general you do not need to check for key down characters, since most CEdit classes offer other methods and events for checking the content, and the CDialog class offers the UpdateData()[^] method to capture all fields.

        One of these days I'm going to think of a really clever signature.

        M Offline
        M Offline
        Member 2119844
        wrote on last edited by
        #5

        well, this is what i have to complete task. it is required.

        L 1 Reply Last reply
        0
        • M Member 2119844

          well, this is what i have to complete task. it is required.

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #6

          Fine, you do it any way you want.

          One of these days I'm going to think of a really clever signature.

          1 Reply Last reply
          0
          • M Member 2119844

            yes i have add InFlag = true; it is not working.

            CString CMyTextBox::GetString()
            {
            // how to come here after press enter by user that is main thing
            if (InFlag == true)
            return RetVal;
            return "";
            }

            it is call form dialog class here is code.

            void CRetValTestDlg::OnGetString()
            {
            m_TxInput.InFlag = false;
            CString s = m_TxInput.GetString();
            m_TxOutput.SetWindowText (s);
            }
            void CRetValTestDlg::OnGetNumber()
            {
            m_TxInput.InFlag = false;
            CString s;
            double d = m_TxInput.GetNumber();
            s.Format ("%d",d);
            m_TxOutput.SetWindowText (s);
            }

            i would like to upload project file for review. if possible.

            S Offline
            S Offline
            Santhosh G_
            wrote on last edited by
            #7

            Few comments. 1) First statement of CRetValTestDlg::OnGetString() set InFlag to false. Therefore m_TxInput.GetString() will return "". Move InFlag = false to CRetValTestDlg::OnGetString(). Accessing a member variable outside of class is not a good. 2) First statement of CRetValTestDlg::OnGetNumber() set InFlag to false. Therefore m_TxInput.GetString() will return 0. Move InFlag = false to CRetValTestDlg::OnGetNumber(). 3) modify CMyTextBox::OnKeyDown() like this

            if (nFlags == 28) // Enter press
            {
            InFlag = true;
            GetWindowText(RetVal);
            }

            1. Need to identify Enter KeyPress in your EditControl. 3) Normally an Edit control will not get Enter key press, On Enter keypress, focused button click action will be performed. One option is to change the style of your Edit control to Multi-line. Change MultiLine to true, or enable ES_MULTILINE for the CMyTextBox window style. Another option is to identify enter keypress from PreTranslateMessage(), if "VK_ENTER" key down is occurred from edit control set your InFlag to true.
            M 1 Reply Last reply
            0
            • S Santhosh G_

              Few comments. 1) First statement of CRetValTestDlg::OnGetString() set InFlag to false. Therefore m_TxInput.GetString() will return "". Move InFlag = false to CRetValTestDlg::OnGetString(). Accessing a member variable outside of class is not a good. 2) First statement of CRetValTestDlg::OnGetNumber() set InFlag to false. Therefore m_TxInput.GetString() will return 0. Move InFlag = false to CRetValTestDlg::OnGetNumber(). 3) modify CMyTextBox::OnKeyDown() like this

              if (nFlags == 28) // Enter press
              {
              InFlag = true;
              GetWindowText(RetVal);
              }

              1. Need to identify Enter KeyPress in your EditControl. 3) Normally an Edit control will not get Enter key press, On Enter keypress, focused button click action will be performed. One option is to change the style of your Edit control to Multi-line. Change MultiLine to true, or enable ES_MULTILINE for the CMyTextBox window style. Another option is to identify enter keypress from PreTranslateMessage(), if "VK_ENTER" key down is occurred from edit control set your InFlag to true.
              M Offline
              M Offline
              Member 2119844
              wrote on last edited by
              #8

              yes i did as you said. the functions does not work. because functions does not wait for Enter. The function call from outer class so that InFlag has to false until press enter but with out press enter function ends. the function is not giving chance to press Enter or type any text to Edit box.

              S 1 Reply Last reply
              0
              • M Member 2119844

                yes i did as you said. the functions does not work. because functions does not wait for Enter. The function call from outer class so that InFlag has to false until press enter but with out press enter function ends. the function is not giving chance to press Enter or type any text to Edit box.

                S Offline
                S Offline
                Santhosh G_
                wrote on last edited by
                #9

                Which function ? On every key press, CMyTextBox::OnKeyDown() will call. But Enter will not called. Its reason is "Enter" keypress will consider as Action of currently focused button. please change the MultiLine property of EditBox and check whether enter is recieved in KEyDown

                M 1 Reply Last reply
                0
                • S Santhosh G_

                  Which function ? On every key press, CMyTextBox::OnKeyDown() will call. But Enter will not called. Its reason is "Enter" keypress will consider as Action of currently focused button. please change the MultiLine property of EditBox and check whether enter is recieved in KEyDown

                  M Offline
                  M Offline
                  Member 2119844
                  wrote on last edited by
                  #10

                  keyDown received Enter. that is not a problem. but if you see my code you will find that function does not wait for enter. function are (double CMyTextBox::GetNumber() and CString CMyTextBox::GetString()) they have to wait until press enter. i think the problem is this. here is link of project file. https://skydrive.live.com/redir?resid=78B222720843DE16!116&authkey=!AAjKOKZHSd-q9lI please kindly check test project.

                  S 1 Reply Last reply
                  0
                  • M Member 2119844

                    keyDown received Enter. that is not a problem. but if you see my code you will find that function does not wait for enter. function are (double CMyTextBox::GetNumber() and CString CMyTextBox::GetString()) they have to wait until press enter. i think the problem is this. here is link of project file. https://skydrive.live.com/redir?resid=78B222720843DE16!116&authkey=!AAjKOKZHSd-q9lI please kindly check test project.

                    S Offline
                    S Offline
                    Santhosh G_
                    wrote on last edited by
                    #11

                    Please move GetWindowText() to CMyTextBox::GetNumber() and CMyTextBox::GetString(). CMyTextBox::OnKeyDown() will not give text values. CRetValTestDlg::OnGetString() still calls m_TxInput.InFlag = false; Please remove it.

                    M 1 Reply Last reply
                    0
                    • S Santhosh G_

                      Please move GetWindowText() to CMyTextBox::GetNumber() and CMyTextBox::GetString(). CMyTextBox::OnKeyDown() will not give text values. CRetValTestDlg::OnGetString() still calls m_TxInput.InFlag = false; Please remove it.

                      M Offline
                      M Offline
                      Member 2119844
                      wrote on last edited by
                      #12

                      no no. this is not a way what i need. i need to get value after press Enter Key.

                      S 1 Reply Last reply
                      0
                      • M Member 2119844

                        no no. this is not a way what i need. i need to get value after press Enter Key.

                        S Offline
                        S Offline
                        Santhosh G_
                        wrote on last edited by
                        #13

                        "After press Enter key" I can undestand that you need to get the String and Number values on pressing Enter key.is it right ?

                        M 1 Reply Last reply
                        0
                        • S Santhosh G_

                          "After press Enter key" I can undestand that you need to get the String and Number values on pressing Enter key.is it right ?

                          M Offline
                          M Offline
                          Member 2119844
                          wrote on last edited by
                          #14

                          Yes. when it call function. like GetString();

                          S 1 Reply Last reply
                          0
                          • M Member 2119844

                            Yes. when it call function. like GetString();

                            S Offline
                            S Offline
                            Santhosh G_
                            wrote on last edited by
                            #15

                            If you can override PreTranslateMessage() in your Dialog class, you can track Enter press from PreTranslateMessage().

                            BOOL CRetValTestDlg::PreTranslateMessage(MSG* pMsg)
                            {
                            if( pMsg->message == WM_KEYDOWN &&
                            pMsg->wParam == VK_RETURN &&
                            pMsg->hwnd == GetDlgItem( IDC_TX_INPUT )->m_hWnd)
                            {
                            // When pressing Enter key in IDC_TX_INPUT edit box, you will get control here.
                            // Return FALSE ensures Dialog will not process this Enter KeyPress

                                // return FALSE;
                            }
                            return CDialog::PreTranslateMessage(pMsg);
                            

                            }

                            M 1 Reply Last reply
                            0
                            • S Santhosh G_

                              If you can override PreTranslateMessage() in your Dialog class, you can track Enter press from PreTranslateMessage().

                              BOOL CRetValTestDlg::PreTranslateMessage(MSG* pMsg)
                              {
                              if( pMsg->message == WM_KEYDOWN &&
                              pMsg->wParam == VK_RETURN &&
                              pMsg->hwnd == GetDlgItem( IDC_TX_INPUT )->m_hWnd)
                              {
                              // When pressing Enter key in IDC_TX_INPUT edit box, you will get control here.
                              // Return FALSE ensures Dialog will not process this Enter KeyPress

                                  // return FALSE;
                              }
                              return CDialog::PreTranslateMessage(pMsg);
                              

                              }

                              M Offline
                              M Offline
                              Member 2119844
                              wrote on last edited by
                              #16

                              yes this is the part which i did not understand. how it goes to GetString or GetNumber function. because PreTranslateMessage in Dlg class. and GetString and GetNumber function in other class. so how it will connect with each other. and how it trap event from one class to other class function. i would like to request for little more detail and step. how it work? if that is just need to get number or string and put in variable it will be ok. but the value should return from function.

                              S 1 Reply Last reply
                              0
                              • M Member 2119844

                                yes this is the part which i did not understand. how it goes to GetString or GetNumber function. because PreTranslateMessage in Dlg class. and GetString and GetNumber function in other class. so how it will connect with each other. and how it trap event from one class to other class function. i would like to request for little more detail and step. how it work? if that is just need to get number or string and put in variable it will be ok. but the value should return from function.

                                S Offline
                                S Offline
                                Santhosh G_
                                wrote on last edited by
                                #17

                                PreTranslateMessage in Dlg class. and GetString and GetNumber function in other class. so how it will connect with each other. You can directly call m_TxInput.GetString() and m_TxInput.GetNumber() from PreTranslateMessage(). If you want to send a message to CMyTextBox class, you can call PostMessage() with a USER function to the edit control.

                                M 1 Reply Last reply
                                0
                                • S Santhosh G_

                                  PreTranslateMessage in Dlg class. and GetString and GetNumber function in other class. so how it will connect with each other. You can directly call m_TxInput.GetString() and m_TxInput.GetNumber() from PreTranslateMessage(). If you want to send a message to CMyTextBox class, you can call PostMessage() with a USER function to the edit control.

                                  M Offline
                                  M Offline
                                  Member 2119844
                                  wrote on last edited by
                                  #18

                                  Thanks. but i don't know about it. is it possible to give one working example. it will be great help for me. please consider i am learning.

                                  L 1 Reply Last reply
                                  0
                                  • M Member 2119844

                                    Thanks. but i don't know about it. is it possible to give one working example. it will be great help for me. please consider i am learning.

                                    L Offline
                                    L Offline
                                    Lost User
                                    wrote on last edited by
                                    #19

                                    I have been following this thread and I think you are painting yourself into a corner. You could make this so much easier for yourself by using the standard messages and notifications available to a dialog and its child controls.

                                    One of these days I'm going to think of a really clever signature.

                                    M 1 Reply Last reply
                                    0
                                    • L Lost User

                                      I have been following this thread and I think you are painting yourself into a corner. You could make this so much easier for yourself by using the standard messages and notifications available to a dialog and its child controls.

                                      One of these days I'm going to think of a really clever signature.

                                      M Offline
                                      M Offline
                                      Member 2119844
                                      wrote on last edited by
                                      #20

                                      well sir, i am just learning and doing work. i don't have much idea about it. so i request for help. i am trying this from all most a week. i google it and did not found simillar to what i am looking for. maybe because i don't have enough knowledge. so.. hoping to get help from some kind heart.

                                      L 1 Reply Last reply
                                      0
                                      • M Member 2119844

                                        well sir, i am just learning and doing work. i don't have much idea about it. so i request for help. i am trying this from all most a week. i google it and did not found simillar to what i am looking for. maybe because i don't have enough knowledge. so.. hoping to get help from some kind heart.

                                        L Offline
                                        L Offline
                                        Lost User
                                        wrote on last edited by
                                        #21

                                        Member 2119844 wrote:

                                        hoping to get help from some kind heart.

                                        Which is what I offered to you yesterday. Use the features of the CDialog and its controls rather than struggling to try and interpret individual keystrokes that are already captured for you. Text boxes have events that you can capture as characters are entered into them, which allow you to check and/or modify their content. The CDialog class has a function that will capture or refresh all the data between the dialog and the associated class variables etc.

                                        One of these days I'm going to think of a really clever signature.

                                        M 1 Reply Last reply
                                        0
                                        • L Lost User

                                          Member 2119844 wrote:

                                          hoping to get help from some kind heart.

                                          Which is what I offered to you yesterday. Use the features of the CDialog and its controls rather than struggling to try and interpret individual keystrokes that are already captured for you. Text boxes have events that you can capture as characters are entered into them, which allow you to check and/or modify their content. The CDialog class has a function that will capture or refresh all the data between the dialog and the associated class variables etc.

                                          One of these days I'm going to think of a really clever signature.

                                          M Offline
                                          M Offline
                                          Member 2119844
                                          wrote on last edited by
                                          #22

                                          Dear sir, i really appreciate your and Santhosh suggestion. i do. but the thing is bit difference what i am looking for. maybe i am not able to describe very well, what i am looking for. sorry for that. because my English is not good enough. so please don't get me wrong. once again i am trying to explain what i need to do. and what i come up to. i need to make function that return value after press Enter key. not directly and not in any other way. but from function when it call from outer class. this is the event when user press button in dlg class.

                                          void CRetValTestDlg::OnGetString()
                                          {
                                          m_TxInput.InFlag = false;
                                          CString s = m_TxInput.GetString();
                                          m_TxOutput.SetWindowText (s);
                                          }

                                          and this is CMyTextBox calss member function which is call from dlg class on button press. which need to return value.

                                          CString CMyTextBox::GetString()
                                          {
                                          if (InFlag == true)
                                          return RetVal;
                                          return "";
                                          }

                                          now i have come upto this

                                          CString CMyTextBox::GetString()
                                          {
                                          while (InFalg != true) // this is infinite loop but
                                          {
                                          DoEvents(); // doEvents hole for it so it wait until it get event
                                          if (InFlag == true) // the InFlag = true when user press Enter;
                                          return RetVal; // and return value
                                          }
                                          return ""; // else return "";
                                          }

                                          this is work. but when i use While and DoEvents the processor took lot of work. it took all most 50% of CPU used. and which is really not good. i think. and some time it does not come out from that loop when user left so when the program close. it remain in task manager. and CUP running. now What i am looking for is -> is there any other way to wait till event. maybe using CEvent or CallBack or threading or something else. without using lot of CPU used. because i don't know anything about them. i have never use.

                                          CString CMyTextBox::GetString()
                                          {
                                          //====================== Here i am looking for help =============================
                                          // wait for event here or WaitTill (InFlag !=true); in this line i need help.
                                          //==================================================================================
                                          if (InFlag == true)
                                          return RetVal;
                                          return "";
                                          }

                                          i hope this time i could able to tell what i need for. this is really important for me. so Please Please Help.

                                          L 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