How to set int or double variable entered in editbox control

Ok i'm new to this win32 api (not MFC).So as my first project is a calculator.
The question is how to get the int or double value of the editbox control.
I have good knowledge of c++.
Last edited on
Last edited on
I don't understand this how MSDN explain it.
Please note that I may get some TCHAR functions wrong as I don't remember the exact syntax now.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
HWND someEditControl;
/*initialize control & stuff*/

/*maximum number of (w)chars you want to get from the edit box control, 
for example you can use this to round a float/double a few decimals*/
constexpr BYTE maxChars = 10; 

TCHAR * buffer = new TCHAR[maxChars]; /* buffer to hold the content you'll get */

/* get up to maxChars (w)chars from the edit control into your buffer */
int writtenChars = GetWindowText(someEditControl, buffer, maxChars); 

/* convert the buffer, but remember that the control may contain less (w)chars 
than the maximum you want, writtenChars will contain the exact number */
float editBoxContent = _tcstof(buffer, NULL);

delete[] buffer; //don't forget to delete buffer  
Last edited on
I would do it like this:
1
2
3
4
5
6
7
8
9
10
11
12
HWND hEdit; // your edit window
const int BUFFER_SIZE = 10;
std::string buffer(BUFFER_SIZE, 10);
int len = GetWindowTextA(hEdit, &buffer[0], BUFFER_SIZE);
if (len == 0)
{
  MessageBoxA(0, "Text was empty", "ERROR", MB_OK | MB_ICONASTERISK);
  return FALSE;          
}
buffer.resize(len);
MessageBoxA(0, buffer.c_str(), "Number as text", MB_OK);
int num = stoi(buffer);
Thank you but it does calculate.I wanted to do addition operation but it does work.
I hope any you guys can help me.

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
#include <Windows.h>
#define ID_CALCULATE 101

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

HWND hwnd,editnum1,editnum2,edittotal,buttoncalclulate;
WNDCLASSEX wc;
MSG msg;
HINSTANCE hinst;
int APIENTRY WinMain(HINSTANCE hinst, HINSTANCE hprev, LPSTR line, int show)
{
	wc.style = CS_HREDRAW | CS_VREDRAW;
	wc.cbSize = sizeof(WNDCLASSEX);
	wc.cbClsExtra = 0;
	wc.cbWndExtra = 0;
	wc.hInstance = hinst;
	wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
	wc.hCursor = LoadCursor(NULL, IDI_APPLICATION);
	wc.hbrBackground = (HBRUSH)(COLOR_WINDOW );
	wc.lpszClassName = TEXT("simplecalculator");
	wc.lpszMenuName = NULL;
	wc.lpfnWndProc = WndProc;
	wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);

	RegisterClassEx(&wc);
	hwnd = CreateWindowEx(NULL, TEXT("simplecalculator"), TEXT("Simple Calculator"),WS_SYSMENU|WS_MINIMIZEBOX, CW_USEDEFAULT, CW_USEDEFAULT, 790, 150, NULL, NULL, hinst, NULL);
	ShowWindow(hwnd, show);
	UpdateWindow(hwnd);

	while (GetMessage(&msg, NULL, 0, 0))
	{
		TranslateMessage(&msg);
		DispatchMessage(&msg);

	}
	return msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
{
	

	switch (msg)
	{
	case WM_CREATE:
		editnum1 = CreateWindow(TEXT("edit"), NULL, WS_CHILD | WS_VISIBLE, 20, 20, 200, 30,hwnd, NULL, hinst, NULL);
		editnum2 = CreateWindow(TEXT("edit"), NULL, WS_CHILD | WS_VISIBLE, 230, 20, 200, 30, hwnd, NULL, hinst, NULL);
		edittotal = CreateWindow(TEXT("edit"), NULL, WS_CHILD | WS_VISIBLE, 550, 20, 200, 30, hwnd, NULL, hinst, NULL);
		buttoncalclulate = CreateWindow(TEXT("button"), "Calculate", WS_CHILD | WS_VISIBLE|BS_FLAT, 440, 20, 100, 30, hwnd, (HMENU)ID_CALCULATE, hinst, NULL);
		break;
	case WM_COMMAND:
		if (LOWORD(wparam) == ID_CALCULATE)
		{
			BYTE maxChars = 10;
			TCHAR * buffer = new TCHAR[maxChars]; 		
			int writtenChars = GetWindowText(editnum1, buffer, maxChars);
			float num1 = _tcstof(buffer, NULL);

			BYTE maxChars2 = 10;
			TCHAR * buffer2 = new TCHAR[maxChars2];
			int writtenChars2 = GetWindowText(editnum1, buffer2, maxChars2);
			float num2 = _tcstof(buffer2, NULL);

			float total = num1 + num2;
			char display[1];
			_itoa(total, display, 100000);

			SetWindowTextA(edittotal, display);
         }
		break;
	case WM_DESTROY:
		PostQuitMessage(0);
		break;
	default:
		return DefWindowProc(hwnd, msg, wparam, lparam);
		break;
	}
	return 0;
}
Last edited on
char display[1];
How many digits do you think you can store in it ?
does matter how much i set on the braket it still doesn't work.
Last edited on
What do you mean it doesn't work?
Any more crashes, wrong result, no output ????
i mean if i input 1 in first edit box and 5 in second editbox and i click calculate button which should do 1+5 but the result i got in third editbox is 2.
Yes, it does work fine, only that you read the same control box twice, so it's 1 + 1 = 2.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

			BYTE maxChars = 10;
			TCHAR * buffer = new TCHAR[maxChars]; 		
			int writtenChars = GetWindowText(editnum1, buffer, maxChars);
			float num1 = _tcstof(buffer, NULL);

			BYTE maxChars2 = 10;
			TCHAR * buffer2 = new TCHAR[maxChars2];
			int writtenChars2 = GetWindowText(editnum1, buffer2, maxChars2);
			float num2 = _tcstof(buffer2, NULL);

			float total = num1 + num2;
			char display[1];
			_itoa(total, display, 100000);

			SetWindowTextA(edittotal, display);


Also some more flaws in your code:

Too many global variables:
1
2
3
4
HWND hwnd,editnum1,editnum2,edittotal,buttoncalclulate;
WNDCLASSEX wc;
MSG msg;
HINSTANCE hinst;


You never delete the buffers you put on the heap.
1
2
TCHAR * buffer = new TCHAR[maxChars];
// you should use delete[] buffer or just allocate it on the stack 


Last edited on
oh no didn't realize my mistake until you highlight both of the editnum1.This going to be a valuable lesson so i can pay more attention to the code.Thank you very much Golden Lizard and
Thomas 1965.

But if num1+num2=>10 it shows alphabet
Last edited on
You never delete the buffers you put on the heap.

@Golden Lizard,
what do you expect ? He is using your code.
that's just a snippet, I thought deleting buffers would be a no brainer as he said he had "good knowledge of c++"
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
BYTE maxChars = 1000000;
			TCHAR * buffer = new TCHAR[maxChars]; 		
			int writtenChars = GetWindowText(editnum1, buffer, maxChars);
			float num1 = _tcstof(buffer, NULL);

			BYTE maxChars2 = 100000;
			TCHAR * buffer2 = new TCHAR[maxChars2];
			int writtenChars2 = GetWindowText(editnum2, buffer2, maxChars2);
			float num2 = _tcstof(buffer2, NULL);

			float total = num1 + num2;
			char display[10];
			_itoa(total, display, 10);

			SetWindowTextA(edittotal, display);


I did some change to the code now it does't show alphabet anymore.

Thank you very much for your guide.

I'm sorry for anything you don't like from me.

Now I understand why video games are so expansive.
No need for 2 variables if you use the same length twice. Also 100000 is way too much and pretty wasteful in your context.
Last edited on
Well this winapi programming is so complicated.Can you recommend any good book about it.
which one is better
They complement each other. The first is for learning, the second more for reference.
Topic archived. No new replies allowed.