[try Beta version]
Not logged in

 
Crash on dynamically adding buttons

Jun 18, 2019 at 5:44pm
Each time clicking on button "+", a new button "G" comes up (no maximum applies). Each time clicking on button "G", a new button "GTR" comes up (maximum is 4 for each button "G"). On click of a new "GTR" button, an empty canvas for drawing and dragging circles comes up. These circles must be remembered when clicking on another "GTR" button, which is implemented by the for loop. However, the program crashes after clicking on the third or fourth time clicking on a new "GTR" button. Am I overlooking something? Is my approach for dynamically adding buttons the right way? What is causing the crash?

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
case WM_CREATE: {
	CreateWindow (
	"button",
	"+",
	WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
	20,
	90,
	30,
	30,
	hwnd,
	(HMENU) 1000,
	((LPCREATESTRUCT) lParam)->hInstance,
	 NULL
	);
break;
}

case WM_COMMAND: {
	wmId    = LOWORD(wParam);
	wmEvent = HIWORD(wParam);
	if(LOWORD(wParam) == 1000){//"+"
		mypage++;
		if(mystack0 == 4){
			mypage2++;
			mystack0 = 0;
		}
        }
	if(LOWORD(wParam) == 1000){//"+"
		CreateWindow (
		"button",
		"G",
		WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
		25,
		140,
		20,
		20,
		hwnd,
		(HMENU) 10001,
		NULL,
		NULL
		);
	}
if(LOWORD(wParam) == 10001){//"G"
	if(mystack0 < 4){
		CreateWindow (
			"button",
			"GTR",
			WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
			25,
			165,
			95,
			30,
			hwnd,
			(HMENU) (10005+mystack),
			NULL,
			NULL
			);
			mystack0 += 1;
			mystack += 1;
			X.push_back(0);
		    	Y.push_back(0);
		}
	}
        for(int i = 0; i < mystack; i++){//"GTR"
		if(LOWORD(wParam) == (10005+i)){
			RedrawWindow(hwnd, &lprcUpdate, hrgnUpdate, RDW_INVALIDATE | RDW_ERASE | RDW_UPDATENOW );
		        break;
		}
	}
        break;
}
Jun 20, 2019 at 11:37pm
Please give me a direction for a solution. When the program keeps on crashing, I cannot go further.
Jun 21, 2019 at 12:11am
Since this is more specialized it's hard to tell what's really happening in this code. If you're using Visual Studio you can use the debugger and see exactly what's happening and which part of your code to find the issue.
Jun 21, 2019 at 12:13am
out here, we'd have to be able to compile a working example, and the code posted is obviously not enough to do that.
Topic archived. No new replies allowed.