You are here:

C++/Check Box problem

Advertisement


Question
QUESTION: I am building a program that analyzes certain aspects of a file and writes a graph showing its findings. With check boxes, I want to be able to pick the parts of a file I want shown on the graph and with the user switching the check boxes on or off, reduce 'clutter'. I also want to be able to disable certain check boxes when they aren't relitive to what I want shown in the graph.

I feel that the best way to accomplish most of this is to have the check boxes in an array. My problem is that I don't know how to create an array of check boxes.

I've used GetDlgItem(IDC_Check0)->GetWindowRect(rectCBx[0]); when sizing a checkbox in another program, but it doesn't seem to work here and draws an error. I've thought of using GetWindow(),but GetDlgItem(IDC_Check0)->GetWindow(CBx[0]; also draws an error.

I'd also like to have some of the check boxes pre-checked by the program. I see in MSDN that there is 'SerCheck'. With no examples, I'm stuck. How is it used?

Thanks in advance for both the time taken to read about my problem and the time taken to reply.
-neilvmcc

ANSWER: Hello Neil.

The GetDlgItem will return a CWnd* which you can cast to CButton* and store in an array or vector of CButton*. Make sure that you are calling GetDlgItem in or after OnInitDialog. I've occasionally made the error of calling it from the Dialog's constructor. I'm assuming here that we are dealing with dialogs. Actually, I have no MFC experience with SDI or MDI projects.

Once you have a CButton* you can set its check with SetCheck. For example, in OnInitDialog (or the equivalent in your project type), you can have:


CButton *ch = (CButton*)GetDlgItem(IDC_CHECK1);
ch->SetCheck(BST_CHECKED);

You can enable/disable the checkbox with ch->EnableWindow( 1 or 0);

I don't know why you need the window rectangle. Is that related to another question ?

I hope that helps you out.

Best regards
Zlatko

---------- FOLLOW-UP ----------

QUESTION: Hi again, Zlatco.

Your suggestions worked OK until I tried to retrieve and use the array CButton.

I tried: 'CButton *ch[1] = (CButton*)GetDlgItem(IDC_CHECK1);' which drew an error.

I also tried: 'CButton *ch = (CButton*)GetDlgItem(IDC_CHECK1);'(which you wrote) and I added 'int i = CButton[1];' which also drew an error.

Thanks in advance for reading and answering my question,
-Neil

Answer
Hello Neil.

I think you are not using the correct array syntax.

It's like this:

// Declare an array of CButton pointers (32 for example)
CButton *checkBoxes[32];

// Assign checkboxes to the array
checkBoxes[0] = (CButton*)GetDlgItem(IDC_CHECK0);
checkBoxes[1] = (CButton*)GetDlgItem(IDC_CHECK1);

//Now access them
checkBoxes[0]->SetCheck(BST_CHECKED);
checkBoxes[1]->EnableWindow(0);

Does that work for you ?

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Zlatko

Expertise

No longer taking questions.

Experience

No longer taking questions.

Education/Credentials
No longer taking questions.

©2012 About.com, a part of The New York Times Company. All rights reserved.