-
August 23rd, 2006, 06:22 AM
#1
Lock checkbox
Hello,
I want to lock a checkbox in MFC (Visual C++ 6.0). When I use the EnableWindow(FALSE) method, it grays out the whole checkbox. But I need only to disallow a user changing its state - but it should stay white! Like the "Lock" parameter in Microsoft Access does... How can I do this in MFC?
-
August 23rd, 2006, 06:24 AM
#2
Re: Lock checkbox
One way to achieve that is by handling the "check" message and always disabling it. But wouldn;t that be misleading for the user? Having a checkbox enabled yet unusable kinda looks like a bug from the user perspective.
Har Har
-
August 23rd, 2006, 06:28 AM
#3
Re: Lock checkbox
You do this manually in the checkbox event handler.
If the user tries to change the box when it is locked,
restore it with SetCheck() or set the member variable
and use UpdateData(FALSE).
Another option is to block messages in PreTranslateMessage().
Nobody cares how it works as long as it works
-
August 23rd, 2006, 07:01 AM
#4
Re: Lock checkbox
I remembered, long time ago, I had a boss which was not only a UI specialist but also a great aesthete. He was bothered by grayed text of disabled check boxes. It seems your boss has even greater sensitivity to the beauty and art, but also he never thought about the poor user trying to do something "blocked" but shown as allowed.
-
August 23rd, 2006, 07:30 AM
#5
Re: Lock checkbox
No, it's not my boss, it's me! =) Some checkboxes are used to show status. They are not supposed to be changed by the user. But when they are grey, they are just ugly...
Ufff... But why is it so complicated to do such simple thing?! I thought Access was not flexible. But even there you could specify one option only to achieve what I was asking... Nevermind, thank you all! =)
Last edited by Dmitry Perets; August 23rd, 2006 at 07:33 AM.
-
August 23rd, 2006, 07:49 AM
#6
Re: Lock checkbox
Originally Posted by Dmitry Perets
I thought Access was not flexible.
Access indeed is more flexible in making more colored but tricky stuff.
-
August 23rd, 2006, 08:47 AM
#7
Re: Lock checkbox
Hmmm... And how can I cancel the user's action inside the handler? If I use SetCheck(), I should know which state exactly I want to set... But I know only that I want to keep the same state, whatever it was... How can I do this?
-
August 23rd, 2006, 09:11 AM
#8
Re: Lock checkbox
Well, the user has just changed the state, so you could try
Code:
mycb->SetCheck(!mycb->GetCheck());
Nobody cares how it works as long as it works
-
August 23rd, 2006, 09:55 AM
#9
Re: Lock checkbox
Well, the last question for today, I hope...
Is there any elegant way to make this thing more generic? Because now I actually need to add handler for each checkbox... I would prefer to write one handler for everybody. But then the trick with SetCheck() won't work...
-
August 23rd, 2006, 10:44 AM
#10
Re: Lock checkbox
Yes, possibly using
Code:
ON_CONTROL_RANGE(BN_CLICKED, IDC_FIRSTCHECKBOX, IDC_LASTCHECKBOX, OnButtonClicked)
afx_msg void OnButtonClicked( UINT nID );
void CMyDialog::OnButtonClicked( UINT nID )
{
CButton *mycb=(CButton *)GetDlgItem(nID);
mycb->SetCheck(!mycb->GetCheck());
}
This requires all button control IDs to be a continuous sequence.
Nobody cares how it works as long as it works
-
August 24th, 2006, 04:38 AM
#11
Re: Lock checkbox
Yeah, it works! Thank you for this great tutorial on VC++ and MFC =) As you understand, I never worked with it before...
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|