How do I make MFC checkbox read-only but keep text enabled?
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).
- Added a couple checkboxes.
- Set the checkbox captions to nothing.
- Set checkbox transparent property to true.
- Add a couple labels beside checkbox.
- Change transparent property of labels to true.
- 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'开发者_Go百科m new to MFC (I'm a C# guy) so maybe I'm missing something.
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());
}
Just make use of the Auto
property, and leave it unchecked.
When clicking on it, it will not toggle any more, so basically it will be read-only, but still work fine as output.
Check Box Properties: Styles Auto Creates a check box that, when selected, automatically toggles between checked and unchecked states. You must set this property to True if you are using a group of check boxes with Dialog Data Exchange.
Type: Bool
,Default: True.
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;
}
You could un-select the box in the on-click function unless another condition exists.
void SetCheckBoxReadOnly(CButton* i_checkBox, bool i_readOnly)
{
if (!i_checkBox)
{
return;
}
// Clear/set "Auto" property
if (i_readOnly)
{
i_checkBox->ModifyStyle(BS_AUTOCHECKBOX, BS_CHECKBOX);
}
else
{
i_checkBox->ModifyStyle(BS_CHECKBOX, BS_AUTOCHECKBOX);
}
// Set a grey background for check square - looks like disabled :)
i_checkBox->SetState(i_readOnly ? TRUE : FALSE);
}
精彩评论