MFC check box question

This is a discussion on MFC check box question within the Windows Programming forums, part of the Platform Specific Boards category; Hello, I am somewhat new to windows programming and was wondering if someone could answer a question I have about ...

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    34

    MFC check box question

    Hello, I am somewhat new to windows programming and was wondering if someone could answer a question I have about some strange behavior. I have a row of check boxes that I am using to check individually, and also one check box called all that will check or uncheck all of the other boxes, according to if it is checked or unchecked itself. Here's what I am doing for normal check boxes.

    What this does (or is supposed to do) is if the check is already set, unset it and set the member variable to false, otherwise it must be unchecked so we check it and set to true.
    Code:
            CButton *btnPtr = (CButton *) GetDlgItem(IDC_CHECK_1);
    	if (btnPtr->GetCheck()){
    		m_check1 = false;
    		btnPtr->SetCheck(0);
    	}
    	else{
    		m_check1 = true;
    		btnPtr->SetCheck(1);
    	}
    That code is in the single checkboxes, like check1, check2, check3, etc. However, it does not seem to work. When the box is unchecked, it goes to if and if it's checked it goes to the else. Now comes the strange part. In the check all check box I have code that looks like this.
    Code:
    m_check1_all = true;
    CApplicationNameDlg::OnBnClickedCheck1();
    CApplicationNameDlg::OnBnClickedCheck2();
    CApplicationNameDlg::OnBnClickedCheck3();
    When that code executes all the check boxes actually check and behave how I want them to. However when trying to check them individually they seem to function in the opposite manner. Does anyone know what could be the problem? Thank you.

  2. #2
    Registered User
    Join Date
    Dec 2007
    Posts
    214
    When it goes into the button handler code, the GetCheck is returning the new state of the checkbox. Basically you are stopping it from doing what it wants to do. In your if statement block comment out your code and put a message box that displays "Checking", and in the else statement block do the same with a message box for "Unchecking". Then test.

    However, you'll have to change your checkall function. Basically just set the check variables and call setcheck with appopriate values based on the checkall buttons state.

    I also would make member variables for all the check boxes. It will make your code a little easier in your checkall function. You won't have to GetDlgItem four times.
    Last edited by DaveH; 03-09-2009 at 01:14 PM.

  3. #3
    Registered User
    Join Date
    Feb 2005
    Posts
    34
    Thank you so much Dave. I had not thought about the nature of the OnBnClicked function; returning the new state that had just resulted from clicking the button that is. This was in fact the problem. Thanks again for the quick response, it is very much appreciated.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question under MFC...
    By NewbieStats in forum C++ Programming
    Replies: 0
    Last Post: 03-30-2005, 02:24 PM
  2. MFC Open directory dialog box
    By bennyandthejets in forum Windows Programming
    Replies: 2
    Last Post: 06-01-2004, 05:28 AM
  3. Check Box Question
    By Yatta in forum Windows Programming
    Replies: 5
    Last Post: 07-14-2002, 08:01 PM
  4. WIndows programming?
    By hostensteffa in forum Windows Programming
    Replies: 7
    Last Post: 06-07-2002, 08:52 PM
  5. Tab Controls - API
    By -KEN- in forum Windows Programming
    Replies: 7
    Last Post: 06-02-2002, 09:44 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21