CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11

Thread: Lock checkbox

  1. #1
    Join Date
    Apr 2005
    Posts
    33

    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?

  2. #2
    Join Date
    Aug 2002
    Location
    Cluj-Napoca,Romania
    Posts
    3,496

    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

  3. #3
    Join Date
    Jun 2002
    Location
    Stockholm, Sweden
    Posts
    1,641

    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

  4. #4
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,234

    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.
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

  5. #5
    Join Date
    Apr 2005
    Posts
    33

    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.

  6. #6
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,234

    Re: Lock checkbox

    Quote Originally Posted by Dmitry Perets
    I thought Access was not flexible.
    Access indeed is more flexible in making more colored but tricky stuff.
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

  7. #7
    Join Date
    Apr 2005
    Posts
    33

    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?

  8. #8
    Join Date
    Jun 2002
    Location
    Stockholm, Sweden
    Posts
    1,641

    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

  9. #9
    Join Date
    Apr 2005
    Posts
    33

    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...

  10. #10
    Join Date
    Jun 2002
    Location
    Stockholm, Sweden
    Posts
    1,641

    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

  11. #11
    Join Date
    Apr 2005
    Posts
    33

    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
  •  





Click Here to Expand Forum to Full Width

Featured