从 MFC 和 VS2010 中的编辑控件中读取文本 [英] Read text from edit control in MFC and VS2010

查看:19
本文介绍了从 MFC 和 VS2010 中的编辑控件中读取文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个带有对话框窗口和一些按钮的简单 MFC 应用程序.我还添加了一个编辑控件,以便用户插入文本字符串.

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.

我在文本编辑控件中添加了一个变量名称,即 filepath1,这是代码:

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
", txtname); // but i always read just a "."

推荐答案

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

注意大写的S"

或者你可以投射:

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

最好使用编辑控件.要创建CEdit变量,在VS中的编辑框上右击选择添加成员变量",给变量起个名字,然后点击确定.

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()

这篇关于从 MFC 和 VS2010 中的编辑控件中读取文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆