Checkbox in my program

Pages: 12
Ok so i have a checkbox in my program but it wont check and uncheck. How do i make it do this with as little code as humanly possible?

Can someone either add the code thats needed to make it work to my code or show me what needs to be added to the code and what lines they need to go on.

Here is my code:

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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#define WIN32_LEAN_AND_MEAN
#define _WIN32_WINNT 0x0500
#include <windows.h>
#include "resource.h"


HINSTANCE hInst;

BOOL CALLBACK DialogProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{

    switch(uMsg)
    {
        case WM_INITDIALOG:
            //Blank
            return TRUE;

        case WM_CLOSE: // If the user clicks the X to close the dialog box
            EndDialog(hwndDlg, 0);
            return TRUE;

        case WM_COMMAND:
            switch(LOWORD(wParam))
            {
                case IDC_BTN_QUIT:
                    EndDialog(hwndDlg, 0);
                    return TRUE;

                ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
                //MB_ICONINFORMATION is the type of icon displayed in the window that pops up. There is also//
                //MB_ICONHAND, MB_ICONSTOP, or MB_ICONERROR - Error Icon                                           //
                //MB_ICONQUESTION - Question mark Icon                                                                          //
                //MB_ICONEXCLAMATION or MB_ICONWARNING - Exclamation Icon                                       //
                //MB_ICONASTERISK or MB_ICONINFORMATION - Asterisk Icon                                            //
                /////////////////////////////////////////////////////////////////////////////////////////////////////////////

                case IDC_BTN_TEST:
                    MessageBox(hwndDlg, "Your computer will now crash because you suck.", "Information", MB_ICONERROR);
                    return TRUE;

                case IDC_BTN_TESTBTN2:
                    MessageBox(hwndDlg, "Rated Argh for pirates fuck you.", "Information", MB_ICONWARNING);
                    return TRUE;

                case Button:
                    MessageBox(hwndDlg, "Test Button 2", "Information", MB_ICONINFORMATION);
                    return TRUE;

                case CheckB:
                    BS_AUTOCHECKBOX | WS_VISIBLE | WS_CHILD;
            }
    }

    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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include "resource.h"

//6 is the X position of the dialog box on the screen
//5 is the Y position of the dialog box on the screen
//304 is the width of the dialog box
//206 is the height of the dialog box

DLG_MAIN DIALOGEX 6, 5, 304, 206

CAPTION "Project Manager" //Title of the window

FONT 8, "Ariel" //Font size (8) and type (Tahoma)

STYLE 0x10CE0804

//138,  5, 46, 15
//138 is the X position of the button, Left and right
//5 is the Y position of the button, Up and down
//46 is the buttons length
//15 is the buttons height
//&Test is the name of the button
//IDC_BTN_TEST is the name of the defined keyword in resource.h

/*
BEGIN
  CONTROL "&Test", IDC_BTN_TEST, "Button", 0x10010000, 138,  5, 46, 15
END
*/

//CTEXT - Places text in the window, but not a text box.
//PUSHBUTTON - Similar to CONTROL
//RADIOBUTTON - Creates a Radio Button
//CHECKBOX - Creates a checkbox


BEGIN
  CTEXT "Project Manager Version 1.0.0", IDC_STATIC, 113, 0, 100, 15
  CONTROL "&Test", IDC_BTN_TEST, "Button", 0x10010000, 138,  15, 46, 15
  CONTROL "&Quit", IDC_BTN_QUIT, "Button", 0x10010000, 138, 49, 46, 15
  CONTROL "&Button", IDC_BTN_TESTBTN2, "Button", 0x10010000, 138, 32, 46, 15
  GROUPBOX "GroupBox", IDC_STAT2, 25, 5, 100, 50
  PUSHBUTTON "HI", Button, 50, 25, 46, 15
  RADIOBUTTON "Radio Test", RadioB, 138, 88, 50, 50
  CHECKBOX "Checkbox", CheckB, 138, 120, 42, 50
END



Resource.r:

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

// ID of Main Dialog
#define DLG_MAIN 101

// ID of Button Controls
#define IDC_BTN_TEST 1001
#define IDC_BTN_QUIT 1002
#define IDC_BTN_TESTBTN2 1003
#define IDC_STATIC 1004
#define IDC_STAT2 1005
#define Button 1006
#define RadioB 1007
#define CheckB 1008
#define MI 1009
#define listbox 1010 
NOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO!!!! That doesnt help, i dont know where to put it, if theres anything else i need to do or anything.

"Can someone either add the code thats needed to make it work to my code or show me what needs to be added to the code and what lines they need to go on."
On line 44 of your resource.rc put AUTOCHECKBOX "Checkbox", CheckB, 138, 120, 42, 50 instead.
Oh my god, thank you sooo much!! it works now!now my last question is in the Case statements how do i check if its checked or unchecked because what im going to do is use it for a quiz.
Please help!
So how do i use this? I looked at the link but i dont really get where it goes or how to get it to understand if its checked.
Something like this:
1
2
3
4
5
6
7
8
if (BST_CHECKED == IsDlgButtonChecked(hDlg, CheckB))
{
// checkbox is checked
} else {
// checkbox is unchecked
}

Ahh, i get it now, thanks, but i got an error, it says that hDlg was not declared in the scope, i changed hDlg to NULL and it sort of worked, but when i check and uncheck it, in the CMD box it keeps saying Unchecked, when really its checked:

C:\Users\Chay Hawk\Desktop\C++\Project Manager\main.cpp||In function 'BOOL DialogProc(HWND__*, UINT, WPARAM, LPARAM)':|
C:\Users\Chay Hawk\Desktop\C++\Project Manager\main.cpp|51|error: 'hDlg' was not declared in this scope|
||=== Build finished: 1 errors, 0 warnings ===|


Code:

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
#define WIN32_LEAN_AND_MEAN
#define _WIN32_WINNT 0x0500
#include <windows.h>
#include "resource.h"
#include <iostream>

HINSTANCE hInst;

BOOL CALLBACK DialogProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{

    switch(uMsg)
    {
        case WM_INITDIALOG:
            //Blank
            return TRUE;

        case WM_CLOSE: // If the user clicks the X to close the dialog box
            EndDialog(hwndDlg, 0);
            return TRUE;

        case WM_COMMAND:

            switch(LOWORD(wParam))
            {
                case IDC_BTN_QUIT:
                    EndDialog(hwndDlg, 0);
                    return TRUE;

                ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
                //MB_ICONINFORMATION is the type of icon displayed in the window that pops up. There is also//
                //MB_ICONHAND, MB_ICONSTOP, or MB_ICONERROR - Error Icon                                           //
                //MB_ICONQUESTION - Question mark Icon                                                                          //
                //MB_ICONEXCLAMATION or MB_ICONWARNING - Exclamation Icon                                       //
                //MB_ICONASTERISK or MB_ICONINFORMATION - Asterisk Icon                                            //
                /////////////////////////////////////////////////////////////////////////////////////////////////////////////

                case IDC_BTN_TEST:
                    MessageBox(hwndDlg, "Your computer will now crash because you suck.", "Information", MB_ICONERROR);
                    return TRUE;

                case IDC_BTN_TESTBTN2:
                    MessageBox(hwndDlg, "Rated Argh for pirates fuck you.", "Information", MB_ICONWARNING);
                    return TRUE;

                case Button:
                    MessageBox(hwndDlg, "Test Button 2", "Information", MB_ICONINFORMATION);
                    return TRUE;

                case CheckB:
                    if (BST_CHECKED == IsDlgButtonChecked(NULL, CheckB))
                    {
                    std::cout << "checked" << std::endl;
                    } else {
                    std::cout << "Unchecked" << std::endl;
                    }

            }
    }

    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);
}
Last edited on
hDlg is hwndDlg in your case, the first parameter in your DialogD Procedure.
Ok it works perfect now. How would i grey out a checkbox when another is checked? I dont want the quiz taker checking more than 1 choice. I could use Radio buttons and i have tested them to work 100% but the concept is the same.
Use EnableWindow.
So it would look something like:

case CheckBox1:
EnableWindow(CheckBox1, FALSE);
break;

??????
Ok i tried:

1
2
3
case CheckBox1:
EnableWindow(CheckBox1, FALSE);
break;


and no luck, what else do i need to do?
Last edited on
Please help
If you want the user to just select one on a set of items, then you should be using a radio button. That's what they're designed for, and why there are radio buttons and check boxes.

Using a set of radio buttons signals to the user that they should choose one of the options rather than treating them separately (like check boxes).

Ideally you program would stick to the Windows UX idiom!

Andy

PS That is, you shouldn't be trying to fix the "problem" that checkboxes don't behave like radio buttons. Just use the right kind of controls.
Last edited on
I know I can use radio buttons but im going to need to grey out things lime buttons and such anyways so I might as well learn it now.
Please help i want to get this done!! :(

I just need to know which arguments i need for this, i have been trying it for a few days and its driving me craze, im not sure what other arguments i need to put in it.

case CheckBox1:
EnableWindow(CheckBox1, FALSE);
break;
Bump
Pages: 12