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. To ask the user information via keyboard

To ask the user information via keyboard

Scheduled Pinned Locked Moved C / C++ / MFC
c++help
10 Posts 3 Posters 1 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.
  • A Offline
    A Offline
    antonio343
    wrote on last edited by
    #1

    Hello I want to ask information via keyboard the user. I did a dialog with edit control, this dialog let me to ask the name of the file when the user will kept information. I made this, but not run Code:

    void CDlgResultados::OnBnClickedButton1()
    {

    CDlgSaveLoad dlgSL1;
    
    
    if(dlgSL1.DoModal()==IDOK)
    {
    dlgSL1.GetDlgItemText(IDC\_EDIT1, nombre);
    }
    SaveToFile();
    

    }

    App is in mfc. The error appear in dlgSL1.GetDlgItemText(IDC_EDIT1, nombre);

    M 1 Reply Last reply
    0
    • A antonio343

      Hello I want to ask information via keyboard the user. I did a dialog with edit control, this dialog let me to ask the name of the file when the user will kept information. I made this, but not run Code:

      void CDlgResultados::OnBnClickedButton1()
      {

      CDlgSaveLoad dlgSL1;
      
      
      if(dlgSL1.DoModal()==IDOK)
      {
      dlgSL1.GetDlgItemText(IDC\_EDIT1, nombre);
      }
      SaveToFile();
      

      }

      App is in mfc. The error appear in dlgSL1.GetDlgItemText(IDC_EDIT1, nombre);

      M Offline
      M Offline
      Maximilien
      wrote on last edited by
      #2

      (I will only give partial answer, just to get you going... look for tutorials/sample/example for more complete code and/or documentation) You cannot access dialog control before a DoModal (the controls are not created yet) or after a DoModal (the control were destroyed). In your dlgSL1 dialog code, add a CString variable and "attach" it to the CEdit (IDC_EDIT1) with DDX_Control with something like:

      DDX_Control(pDX, IDC_EDIT1, m_YouStringVariable);

      This can be done manually or with the wizard or the properies (I think); when that is working properly, you can set/get the variable from the parent dialog (in your case: CDlgResultados::OnBnClickedButton1)

      if(dlgSL1.DoModal()==IDOK)
      {
      nombre= dlgSL1.m_YourStringVariable;
      }

      Another way is to override the CDialog::OnOK in the CDlgSaveLoad class and in there, you can get the string from the CEdit and put it in an intermediate variable:

      void CDlgSaveLoad::OnOK()
      {
      // m_YourEdit is a member variable of type CEdit
      // m_YourStringVariable is a member variable of type CString.
      m_YourEdit.GetWindowText(m_YourStringVariable);

      CDialog::OnOK();
      }

      and the code in CDlgResultados::OnBnClickedButton1 will be similar as above. Max.

      Watched code never compiles.

      M 1 Reply Last reply
      0
      • M Maximilien

        (I will only give partial answer, just to get you going... look for tutorials/sample/example for more complete code and/or documentation) You cannot access dialog control before a DoModal (the controls are not created yet) or after a DoModal (the control were destroyed). In your dlgSL1 dialog code, add a CString variable and "attach" it to the CEdit (IDC_EDIT1) with DDX_Control with something like:

        DDX_Control(pDX, IDC_EDIT1, m_YouStringVariable);

        This can be done manually or with the wizard or the properies (I think); when that is working properly, you can set/get the variable from the parent dialog (in your case: CDlgResultados::OnBnClickedButton1)

        if(dlgSL1.DoModal()==IDOK)
        {
        nombre= dlgSL1.m_YourStringVariable;
        }

        Another way is to override the CDialog::OnOK in the CDlgSaveLoad class and in there, you can get the string from the CEdit and put it in an intermediate variable:

        void CDlgSaveLoad::OnOK()
        {
        // m_YourEdit is a member variable of type CEdit
        // m_YourStringVariable is a member variable of type CString.
        m_YourEdit.GetWindowText(m_YourStringVariable);

        CDialog::OnOK();
        }

        and the code in CDlgResultados::OnBnClickedButton1 will be similar as above. Max.

        Watched code never compiles.

        M Offline
        M Offline
        Madhu Nair 0
        wrote on last edited by
        #3

        +5 for the answer!

        A 1 Reply Last reply
        0
        • M Madhu Nair 0

          +5 for the answer!

          A Offline
          A Offline
          antonio343
          wrote on last edited by
          #4

          Thank you very much. + 10 for the answer. Here is the answer :

          void CDlgSaveLoad::OnBnClickedOk()
          {
          CDialogEx::OnOK();
          m_Edit.GetWindowText(m_strData);
          }

          void CDlgResultados::OnBnClickedButton1()
          {
          CDlgSaveLoad mDlg;
          if ( mDlg.DoModal() == IDOK )
          {
          nombre= mDlg.getData();
          }
          SaveToFile();
          }

          M 1 Reply Last reply
          0
          • A antonio343

            Thank you very much. + 10 for the answer. Here is the answer :

            void CDlgSaveLoad::OnBnClickedOk()
            {
            CDialogEx::OnOK();
            m_Edit.GetWindowText(m_strData);
            }

            void CDlgResultados::OnBnClickedButton1()
            {
            CDlgSaveLoad mDlg;
            if ( mDlg.DoModal() == IDOK )
            {
            nombre= mDlg.getData();
            }
            SaveToFile();
            }

            M Offline
            M Offline
            Maximilien
            wrote on last edited by
            #5

            Swap the two lines in OnBnClickedOk. and I assume that

               nombre= mDlg.getData();
            

            will return m_strData ?

            Watched code never compiles.

            A 1 Reply Last reply
            0
            • M Maximilien

              Swap the two lines in OnBnClickedOk. and I assume that

                 nombre= mDlg.getData();
              

              will return m_strData ?

              Watched code never compiles.

              A Offline
              A Offline
              antonio343
              wrote on last edited by
              #6

              Yes, I forgot write here the method :

              public:
              CString getData() const { return m_strData; }

              A 1 Reply Last reply
              0
              • A antonio343

                Yes, I forgot write here the method :

                public:
                CString getData() const { return m_strData; }

                A Offline
                A Offline
                antonio343
                wrote on last edited by
                #7

                I'm sorry, I have another question. When I show something in Edit control, it appear selected in hightlight blue, how can I modify it?? I'd like that only appear the text but not hightlight

                M 1 Reply Last reply
                0
                • A antonio343

                  I'm sorry, I have another question. When I show something in Edit control, it appear selected in hightlight blue, how can I modify it?? I'd like that only appear the text but not hightlight

                  M Offline
                  M Offline
                  Maximilien
                  wrote on last edited by
                  #8

                  antonio343 wrote:

                  When I show something in Edit control, it appear selected in hightlight blue, how can I modify it??

                  have a look at CEdit::SetSel[^] M.

                  Watched code never compiles.

                  A 1 Reply Last reply
                  0
                  • M Maximilien

                    antonio343 wrote:

                    When I show something in Edit control, it appear selected in hightlight blue, how can I modify it??

                    have a look at CEdit::SetSel[^] M.

                    Watched code never compiles.

                    A Offline
                    A Offline
                    antonio343
                    wrote on last edited by
                    #9

                    Ok, but I didn't call this function in my code, and the text appear selected blue when I show the text. I don't want that the text appear selected I use to show and get the text GetDlgItemText/SetDlgItemText Could be this the problem?

                    A 1 Reply Last reply
                    0
                    • A antonio343

                      Ok, but I didn't call this function in my code, and the text appear selected blue when I show the text. I don't want that the text appear selected I use to show and get the text GetDlgItemText/SetDlgItemText Could be this the problem?

                      A Offline
                      A Offline
                      antonio343
                      wrote on last edited by
                      #10

                      I think that the problen is setfocus() I call this function an it select all the text. I need that the text will be showed without higlight

                      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