SetDlgItemText Not Working

First, here is my FULL code because I doubt that only providing the relevant code is useful because here, I'm not really sure which code is relevant anymore. This problem, to me, is mind-boggling.

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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#include <windows.h>

#define IDM_FILE_EXIT		1002
#define IDM_HELP_ABOUT		1003
#define IDC_MAIN_EDIT		1004
#define IDM_ACTIONS_SETTEXT 1005

int desktopWidth = GetSystemMetrics(SM_CXSCREEN);
int desktopHeight = GetSystemMetrics(SM_CYSCREEN);

LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
    LPSTR lpCmdLine, int nCmdShow)
{
    WNDCLASSEX MyWindowClass;
    HWND hWnd;
    MSG Msg;

    MyWindowClass.cbSize        = sizeof(WNDCLASSEX);
    MyWindowClass.style         = 0;
    MyWindowClass.lpfnWndProc   = WndProc;
    MyWindowClass.cbClsExtra    = 0;
    MyWindowClass.cbWndExtra    = 0;
    MyWindowClass.hInstance     = hInstance;
    MyWindowClass.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
    MyWindowClass.hCursor       = LoadCursor(NULL, IDC_ARROW);
    MyWindowClass.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
    MyWindowClass.lpszMenuName  = NULL;
    MyWindowClass.lpszClassName = L"My Window Class";
    MyWindowClass.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);

    if(!RegisterClassEx(&MyWindowClass))
    {
        MessageBox(NULL, L"Window registration failed!", L"Error!",
            MB_ICONEXCLAMATION | MB_OK);
        return 0;
    }

	hWnd = CreateWindowEx(
        WS_EX_CLIENTEDGE,
		L"My Window Class",
        L"The title/caption of my window",
        WS_OVERLAPPEDWINDOW,
        desktopWidth/4, desktopHeight/4, desktopWidth/2, desktopHeight/2,
        NULL, NULL, hInstance, NULL);

    if(hWnd == NULL)
    {
		MessageBox(NULL, L"Window Creation Failed!", L"Error!",
            MB_ICONEXCLAMATION | MB_OK);
        return 0;
    }

    ShowWindow(hWnd, nCmdShow);
    UpdateWindow(hWnd);

    while(GetMessage(&Msg, NULL, 0, 0) > 0)
    {
        TranslateMessage(&Msg);
        DispatchMessage(&Msg);
    }
    return Msg.wParam;
}

LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    switch(uMsg)
    {
		case WM_CREATE:
			HMENU hMenu, hSubMenu;

			hMenu = CreateMenu();

			hSubMenu = CreatePopupMenu();
			AppendMenu(hSubMenu, MF_STRING, IDM_FILE_EXIT, L"E&xit");
			AppendMenu(hMenu, MF_STRING | MF_POPUP, (UINT)hSubMenu, L"&File");

			hSubMenu = CreatePopupMenu();
			AppendMenu(hSubMenu, MF_STRING, IDM_ACTIONS_SETTEXT, L"Set Te&xt");
			AppendMenu(hMenu, MF_STRING | MF_POPUP, (UINT)hSubMenu, L"A&ctions");

			hSubMenu = CreatePopupMenu();
			AppendMenu(hSubMenu, MF_STRING, IDM_HELP_ABOUT, L"&Help");
			AppendMenu(hMenu, MF_STRING | MF_POPUP, (UINT)hSubMenu, L"&About");

			SetMenu(hWnd, hMenu);


			HFONT hfDefault;
			HWND hEdit;

			hEdit = CreateWindowEx(WS_EX_CLIENTEDGE, L"Edit", L"", 
				WS_CHILD | WS_VISIBLE | WS_VSCROLL | WS_HSCROLL | ES_MULTILINE | ES_AUTOVSCROLL | ES_AUTOHSCROLL,
				0, 0, 100, 100, hWnd, (HMENU)IDC_MAIN_EDIT, GetModuleHandle(NULL), NULL);

			if (!hEdit)
				MessageBox(hWnd, L"Edit box creation failed.", L"", MB_OK | MB_ICONERROR);

			hfDefault = (HFONT)GetStockObject(DEFAULT_GUI_FONT);
			SendMessage(hEdit, WM_SETFONT, (WPARAM)hfDefault, MAKELPARAM(FALSE, 0));
		break;
		case WM_SIZE:
			{
			HWND hEdit;
			RECT rcClient;

			GetClientRect(hWnd, &rcClient);

			hEdit = GetDlgItem(hWnd, IDC_MAIN_EDIT);
			SetWindowPos(hEdit, NULL, 0, 0, rcClient.right, rcClient.bottom, SWP_NOZORDER);
			}
		break;
		case WM_COMMAND:
			{
			switch (wParam)
				{
				case IDM_FILE_EXIT:
					PostQuitMessage(0);
					return 0;
				break;
				case IDM_ACTIONS_SETTEXT:
					hEdit = GetDlgItem(hWnd, IDC_MAIN_EDIT);
					if (!hEdit) {MessageBox(hWnd, L"Failed to retrieve dialog item handle.", NULL, MB_OK | MB_ICONERROR);}
					SetDlgItemText(hWnd, (UINT)hEdit, L"This is some boring old space filler. Haha.");
					return 0;
				break;
				case IDM_HELP_ABOUT:
					MessageBox(hWnd, L"copyright (C) Garret Kade 2009", L"About", MB_OK | MB_ICONINFORMATION);
					return 0;
				break;
				}
			return 0;
			}
		break;
		case WM_CLOSE:
            DestroyWindow(hWnd);
        break;
        case WM_DESTROY:
            PostQuitMessage(0);
        break;
        default:
            return DefWindowProc(hWnd, uMsg, wParam, lParam);
    }
    return 0;
}


The problem is simple: I want the text string I provided to be shown in the main edit box when the menu item IDM_ACTIONS_SETTEXT is clicked.
Ha, solved it myself. I had to replace
1
2
3
4
5
case IDM_ACTIONS_SETTEXT:
					hEdit = GetDlgItem(hWnd, IDC_MAIN_EDIT);
					if (!hEdit) {MessageBox(hWnd, L"Failed to retrieve dialog item handle.", NULL, MB_OK | MB_ICONERROR);}
					SetDlgItemText(hWnd, (UINT)hEdit, L"This is some boring old space filler. Haha.");
					return 0;


with...

1
2
3
case IDM_ACTIONS_SETTEXT:
					SetDlgItemText(hWnd, IDC_MAIN_EDIT, L"This is some boring old space filler. Haha.");
					return 0;


Maybe this can help some other people, because from what I gather through Google search, this is a common problem.
Topic archived. No new replies allowed.