Read text from edit control in MFC and VS2010

42,355

Solution 1

_cprintf("Value %S\n", txtname.GetString());

Note the capital 'S'

or you can cast:

_cprintf("Value %S\n", (LPCTSTR)txtname);

You would be better off using an edit control. To create a CEdit variable, right click on the edit box in VS and select "Add Member Variable", give the variable a name and click OK.

You can then retrieve the text in the edit box like this:

CEdit m_EditCtrl;
// ....
CString filePath1 = m_EditCtrl.GetWindowText()

Solution 2

I think your original code was OK for DDX use and CString. The advice to use a control variable and avoid the DDX/DDV functions is really one of preference and not the issue.

I suspect you are compiling with the UNICODE libraries but explicitly calling an ASCII function _cprintf. UNICODE is held as two bytes, for ASCII characters one of these will be 0. If you pass this to an ASCII string function it will stop after the first character.

If you are using UNICODE then call _cwprintf or use the tchar.h macro _tcprintf which will call the correct version for the compiler switch.

Tip: If you are targeting UNICODE only and will never require MBCS support then avoid using the tchar.h macros as they will obscure any issues with char and TCHAR data type mixing.

Share:
42,355
Marcus Barnet
Author by

Marcus Barnet

Updated on September 05, 2020

Comments

  • Marcus Barnet
    Marcus Barnet over 3 years

    I'm writing a simple MFC application with a Dialog window and some buttons. I added also a edit control in order to let user insert a text string.

    I'd like to read the value which is present in the edit control and to store it in a string but i do not know how to do this.

    I have no compilation error, but I always read only a "." mark.

    I added a variable name to the text edit control which is filepath1 and this is the code:

        // CMFC_1Dlg dialog
        class CMFC_1Dlg : public CDialogEx
        {
        // Construction
        public:
            CMFC_1Dlg(CWnd* pParent = NULL);    // standard constructor
    
        // Dialog Data
            enum { IDD = IDD_MFC_1_DIALOG };
    
            protected:
            virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
    
    
        // Implementation
        protected:
            HICON m_hIcon;
    
            // Generated message map functions
            virtual BOOL OnInitDialog();
            afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
            afx_msg void OnPaint();
            afx_msg HCURSOR OnQueryDragIcon();
            DECLARE_MESSAGE_MAP()
        public:
            afx_msg void OnBnClickedButton1();
            afx_msg void OnBnClickedButton2();
            afx_msg void OnEnChangeEdit1();
            CString filePath1;
        }
    
        //...
    void CMFC_1Dlg::OnSysCommand(UINT nID, LPARAM lParam)
    {
        if ((nID & 0xFFF0) == IDM_ABOUTBOX)
        {
            CAboutDlg dlgAbout;
            dlgAbout.DoModal();
        }
        else
        {
            CDialogEx::OnSysCommand(nID, lParam);
        }
    }
    
        CMFC_1Dlg::CMFC_1Dlg(CWnd* pParent /*=NULL*/)
            : CDialogEx(CMFC_1Dlg::IDD, pParent)
            ,filePath1(("..\\Experiments\\Dirs\\"))
        {
            m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
        }
    
        void CMFC_1Dlg::DoDataExchange(CDataExchange* pDX)
        {
            CDialogEx::DoDataExchange(pDX);
            DDX_Text(pDX, IDC_EDIT1, filePath1);
    
        }
    
        // then i try to get the string value with
        CString txtname=filePath1;
        _cprintf("Value %s\n", txtname); // but i always read just a "."
    
  • Marcus Barnet
    Marcus Barnet almost 12 years
    Yes, now it works. However, i realized that it always print the initial value. If I manually change the text value in the edit control, it continues to display the old value. With: filePath1(("..\\Experiments\\Dirs\\")) If i delete it and i insert a new value, it always print the old one. Is there the possibility to refresh store the new value typed by the user?
  • Chris Dargis
    Chris Dargis almost 12 years
    I'm not sure if this is all the code, but I'm not seeing the CEdit variable. You can use the variable to get text in the edit box with CEdit::GetWindowText(). To create a CEdit variable, right click on the edit box in VS and select "Add Member Variable".
  • Marcus Barnet
    Marcus Barnet almost 12 years
    I have the CEdit filePath1 variable in CMFC_1Dlg : public CDialogEx but I do not know how to use it. Don't I need a string value to store the text?
  • Chris Dargis
    Chris Dargis almost 12 years
    The code you provided shows that filePath1 is of type CString, not CEdit
  • Marcus Barnet
    Marcus Barnet almost 12 years
    Yes i did a mistake when I copied the code. May be a solved with GetDlgItemText(IDC_EDIT1, Edit1); where IDC_EDIT1 is the text box. CString txtname; GetDlgItemText(IDC_EDIT1, txtname); _cprintf("Value %S\n", txtname.GetString()); It seems to work now..