Skip to content

Get TextBox Value and display it to messagebox in MFC

Viewing 9 posts - 1 through 9 (of 9 total)
  • Author
    Posts
  • #191198

    well I’m not a good English speaker but I will try my best to show you how do that, just follow my images 🙂

    1) when you open vc++ 6, it look like pic 1

    2) click on new option from menu, it look like my pic 2

    3) under project name type >>myFirstApp and click ok, after click ok, it look like pic 3

    4) check the option Dialog based and click next, after click next, it look like pic 4

    5) just click next, it look like pic5

    6) just click next again, it look like pic6 and click on finish button

    7) after click on finish button, it look like pic 7, just click on ok

    8) after step 7, it look like pic8

    9) drag a textbox and a button into the form, it look like pic9

    our goal is when we click on button1 we get value of textbox and display it in a messagebox like vb6. to do that, follow step 10

    10) double click on button1, it look like pic10 and just click ok, after click ok, it look like pic11

    we need to declare a variable type string that can hold a textbox value, to do that follow step 11

    11) click on FileView, it look like pic12

    hmmm where can I declare my variable in global like in vb6, follow step 12 baby

    12) in the file explorer, just in left, double click on myFirstAppDlg.h, it look like pic13

    scroll down, after the DECLARE_MESSAGE_MAP(), enter the follow lines of code

    public:
    CString strTextBoxValue;

    it look like pic14 if you do right

    well now I have declared my variable name strTextBoxValue type CString and I want go to the event button click to write the code to put textbox value to this variable, where can I go?, follow step 13

    13) in the file explorer, just in left, double click on myFirstAppDlg.ccp, it look like pic15

    enter these lines of code in void CMyFirstAppDlg::OnButton1()

    CWnd* TextBox1=GetDlgItem(IDC_EDIT1);
    TextBox1->GetWindowText(strTextBoxValue);
    ::AfxMessageBox(strTextBoxValue);

    if you’re in the right way, it look like pic16. all the job is done now, we can test it now to see if it work. How Do I run myFirstApp, just look at pic17(Ctrl +F5 to run it), easy no? enter testing string in textbox and click on button1, the result is at pic18

    if you look at this line >> CWnd* TextBox1=GetDlgItem(IDC_EDIT1);

    where I found the IDC_EDIT1, it’s the id associate with the textbox you added to the form(vb6), in MFC all the controls have a id to identify them like the name you name your control in vb6, MFC identify his controls with id only. well to see what is the id of your textbox or your button1, just double click on file resources.h, it look like pic19

    and what is CWnd, if you want to know it, you have to read msdn from microsoft website

    #191206
    Departure
    Member

    I did everything you said to but i got errors :O(

    Compiling...
    myappfirst.cpp
    c:myappfirstmyappfirstdlg.h(46) : error C2275: 'CString' : illegal use of this type as an expression
    c:program filesmicrosoft visual studiovc98mfcincludeafx.h(368) : see declaration of 'CString'
    c:myappfirstmyappfirstdlg.h(46) : error C2057: expected constant expression
    c:myappfirstmyappfirstdlg.h(46) : error C2501: 'Public' : missing storage-class or type specifiers
    c:myappfirstmyappfirstdlg.h(46) : error C2146: syntax error : missing ';' before identifier 'strTextBoxValue'
    c:myappfirstmyappfirstdlg.h(46) : error C2501: 'strTextBoxValue' : missing storage-class or type specifiers
    myappfirstDlg.cpp
    c:myappfirstmyappfirstdlg.h(46) : error C2275: 'CString' : illegal use of this type as an expression
    c:program filesmicrosoft visual studiovc98mfcincludeafx.h(368) : see declaration of 'CString'
    c:myappfirstmyappfirstdlg.h(46) : error C2057: expected constant expression
    c:myappfirstmyappfirstdlg.h(46) : error C2501: 'Public' : missing storage-class or type specifiers
    c:myappfirstmyappfirstdlg.h(46) : error C2146: syntax error : missing ';' before identifier 'strTextBoxValue'
    c:myappfirstmyappfirstdlg.h(46) : error C2501: 'strTextBoxValue' : missing storage-class or type specifiers
    C:myappfirstmyappfirstDlg.cpp(177) : error C2065: 'strTextBoxValue' : undeclared identifier
    Generating Code...
    Error executing cl.exe.
    
    myappfirst.exe - 11 error(s), 0 warning(s)

    Edit://
    Problem fixed, I had a Capital P in “public” instead of a lower case p.

    Danmm no room for typos in MFC

    #191205

    are you able to make it work like pic18 😉

    #191204
    Departure
    Member

    yes after i changed the “P” in public to “p”

    #191203

    that is the way to get edit control value in MFC properly but try this one, this is the way too get edit control value in c++(WIN32)

    char* strTextBoxVal;
    HWND hTextBox = ::GetDlgItem(this->m_hWnd,IDC_EDIT1);
    int lenght=::GetWindowTextLength(hTextBox);

    strTextBoxVal = new char[lenght + 1]; //allocated memory
    ::SendMessage(hTextBox,WM_GETTEXT,(WPARAM)lenght + 1,(LPARAM)strTextBoxVal);
    ::MessageBox(NULL,strTextBoxVal,strTextBoxVal,MB_OK);
    delete strTextBoxVal; //clean up memory, you have to it manually in c++

    #191202

    two other function to get edit control value

    GetDlgItemInt and GetDlgItemText

    read msdn to know how to use them or ask me 😀

    #191201

    should I continue this with an other method that will get the same result but I think a little bit confusing for vb6 programmer with that method I think I will wait.

    what is that method is created a class derived from CEdit class.

    lets me know if it’s enough for you for now 😀

    #191200
    Departure
    Member

    thanks nano, I will be playing around with it some more today :O)

    great tutorial and I look forward to seeing more :O)

    #191199
    Admin
    Administrator

    YOOOOOO, this is a super kick asz tutorial, damn I can finnaly learn c+++ god bless ya 🙂

Viewing 9 posts - 1 through 9 (of 9 total)
  • You must be logged in to reply to this topic.