How to set text in the text box?

I have a textbox and i can get text from it but i cant figure out how to put text in it, im making a calculator so i want to put text in the box when i hit the corresponding button, this is what i have so far, i tried a bunch of stuff a few days ago, but deleted it all.

Main.cpp:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#define WIN32_LEAN_AND_MEAN

#include <windows.h>

#include "resource.h"

HINSTANCE hInst;

BOOL CALLBACK DialogProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    switch(uMsg)
    {
        case WM_INITDIALOG:
            /*
             * TODO: Add code to initialize the dialog.
             */
            return TRUE;

        case WM_CLOSE:
            EndDialog(hwndDlg, 0);
            return TRUE;

        case WM_COMMAND:
            switch(LOWORD(wParam))
            {
                case one:
                    if(BS_PUSHBUTTON = GetDlgItem(hwndDlg, one));

                        char tmp[MAX_PATH+1] = {0};
                        SetDlgItemText(hwndDlg, 1, tmp, MAX_PATH);
            }
    }

    return FALSE;
}


int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
    hInst = hInstance;

    // The user interface is a modal dialog box
    return DialogBox(hInstance, MAKEINTRESOURCE(DLG_MAIN), NULL, (DLGPROC)DialogProc);
}



Resource.rc:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include "resource.h"

DLG_MAIN DIALOGEX 300, 150, 165, 150

CAPTION "Calculator"

FONT 8, "Tahoma"

STYLE DS_SETFONT | DS_FIXEDSYS | WS_MINIMIZEBOX | WS_CAPTION | WS_SYSMENU

BEGIN
    EDITTEXT 1, 0, 0, 165, 15

    //Line 1
    PUSHBUTTON "1", one, 5, 30, 35, 15
    PUSHBUTTON "2", two, 45, 30, 35, 15
    PUSHBUTTON "3", three, 85, 30, 35, 15
    PUSHBUTTON "4", four, 125, 30, 35, 15

    //Line 2
    PUSHBUTTON "5", five, 5, 50, 35, 15
    PUSHBUTTON "6", six, 45, 50, 35, 15
    PUSHBUTTON "7", seven, 85, 50, 35, 15
    PUSHBUTTON "8", eight, 125, 50, 35, 15

    //Line 3
    PUSHBUTTON "9", nine, 5, 70, 35, 15
    PUSHBUTTON "0", zero, 45, 70, 35, 15
END



Resource.h:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <windows.h>

// ID of Main Dialog
#define DLG_MAIN 101

// ID of Button Controls
#define one 1
#define two 2
#define three 3
#define four 4
#define five 5
#define six 6
#define seven 7
#define eight 8
#define nine 9
#define zero 10
#define Clear 11 
Your GetDlgItem call is useless and wrong.
You are set a empty string always to textbox.
Use WM_SETTEXT, SetWindowText or SetDlgItemText, how you wish ...
So it would be something like:

1
2
char tmp[WM_SETTEXT+1] = {0};
SetWindowText(hwndDlg, 1, tmp, WM_SETTEXT);



?????
Last edited on
1
2
3
4
  case one:
  {
    SetDlgItemText(hwndDlg,one,text); // text would be a cstring defined somewhere else in this case
  } break;


If `text` happened to be a std::string instead of cstring you could also do: SetDlgItemText(hwndDlg,one,text.c_str());

Topic archived. No new replies allowed.