views:

449

answers:

3

It seems that disabling a checkbox through the Disabled property also grays out the caption. Does anyone know how to keep the caption enabled but disable input?

EDIT

Based on Paul's idea, I've done the following (now that I figured out that the static label and checkbox has a transparent property).

  1. Added a couple checkboxes.
  2. Set the checkbox captions to nothing.
  3. Set checkbox transparent property to true.
  4. Add a couple labels beside checkbox.
  5. Change transparent property of labels to true.
  6. Expand the checkboxes to encompass the label (so clicking on the label will trigger the check box to change).

But, this gives me very weird results. When I expand the checkbox over the label, it covers the label even though both are transparent. Again, I'm new to MFC (I'm a C# guy) so maybe I'm missing something.

A: 

You could un-select the box in the on-click function unless another condition exists.

corn3lius
+1  A: 

The quick and simple workaround is to not use the checkbox' text member (set it to ""), size down the checkbox to just the click-able square and simply place a label next to the checkbox.

To get a little fancier you could create a custom control that hosts a checkbox and a label which would enable reuse. It wold also be easier way to make the custom checkbox behave as expected for the end user e.g. being able to set the checkbox to selected or unselected when the label gets clicked as well as the checkbox itself. (The simple solution would not automatically relate the label and the checkbox. You could code that within the form but that might get ugly fast if you tend to reuse the paradigm.)

You could also look around for a 3rd-party checkbox control (there are numerous MFC UI libraries out there) but that might be overkill.

See this pseudo-layout:

You have this: (lone check box control)

[x "checkbox text"]

Lay it out like this: (label control aligned right next to the checkbox)

[x][label: "label text"]

Handle the clicked event of the label with something like:

void OnLabelClick(...) {
    if (checkBox.Enabled)
        checkBox.Checked = !checkBox.Checked;
}
Paul Sasik
This sounds reasonable. Within the code I'm working in now, there are other classes that associate labels and text fields which does similar things that I'm doing. See next comment for more...
bsh152s
However, one thing I've noticed with reducing the checkbox down to the square is that the user now has to click on the square. Before, with the captioned label, they could click on the caption and the checkbox would be changed. A solution would be to keep the checkbox expanded with no caption and put a label on top of it. But, then you get into drawing problems. Is there a a way to set the checkbox color transparent with only the control id? I see options out there that subclass the CButton class but I'm trying to keep changes to a minimum.
bsh152s
Edited original question with what I have tried with no luck.
bsh152s
You're making things a little too complicated. Make the checkbox small (don't stretch out the text part), put a label right next to the checkbox and when the label is clicked (handle the label's click event) reverse the associated checkbox' value. Don't mess any overlay or transparency stuff. It's not necessary.
Paul Sasik
Thanks, sometimes I do think too hard.
bsh152s
A: 

Just override the onClick event and toggle the checkbox back to the way it was before.

void CMyDialog::OnBnClickedMyCheckBox()
{
    m_myCheckBox.SetCheck(!m_myCheckBox.GetCheck());    
}
Dave