CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Nov 2011
    Posts
    72

    creating and accessing a checkbox

    Windows XP Pro, Visual Studio 2008, C++, MFC
    Goal: use some checkboxes
    Details:

    Created a Dialog
    Created a class for the dialog: C_Control_Logging
    Created a checkbox: ID = IDC_CBOX_ENABLE_ALL_LOGGING
    Right clicked checkbox and added a variable: m_cbox_logging_enabled
    In the constructor is the code:
    [code] m_c_box_logging_enabled.SetCheck( BST_CHECKED )[/code}


    The result when stepping over this line of code is a popup saying:
    Debug Assertion Failed!
    The line of code referenced is in afxwin2.inl. I read this as a safety
    check point telling me I have a fundamental error in the checkbox code.

    I am suspecting that I am not getting the variable associated with the checkbox,
    but don't know how to do that.

    when I open the properties of the checkbox I don't recognize anything that
    indicates that this checkbox is checked or not checked.

    How do I get access to this checkbox?
    What should I do different?

    (BTW: I have been searching, but everything I find is either not C++ or involves creating a checkbox from scratch. I have already created a couple using the Dialog utilities and now just need to find out how to access them.)

    Edit: Just so it is said, I can open and close the new dialog box.

    Edit again: After reading some more in my text book, I see that my new class does not have an OnInitDialog() function. I supose that means I created the class incorrectly, but I don't see the error of my ways. The book says I can over-ride it, but I have not been able to find that property box yet.

    Thank you for your time.
    Last edited by bkelly; August 20th, 2012 at 11:34 AM.

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: creating and accessing a checkbox

    The checkbox doesn't exist when the dialog's constructor is called. Override OnInitDialog and put your code after the call to CDialog::OnInitDialog.

  3. #3
    Join Date
    Nov 2011
    Posts
    72

    Re: creating and accessing a checkbox

    OK, I think I understand, but there is no OnInitDialog()(.

    After creating what I thought was a class for the dialog box, I have searched the entire solution and the only OnInitDialog() is the one for the main dialog. There is none for the new dialog box.

    Given a new dialog box with the ID: IDD_DLG_CONTROL_LOGGING
    And within that dialog there is a check box with ID: IDC_CBOX_ENABLE_ALL_LOGGING
    And a class for the dialog box named: C_Control_Logging
    What do I need to do to gain access to the checkboxes.
    My Horton book shows there is a properties sheet that has someting that looks like a method to create an override. But I am unable to find it.
    Last edited by bkelly; August 20th, 2012 at 12:24 PM. Reason: add a phrase

  4. #4
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: creating and accessing a checkbox

    As I said, you need to override it.

  5. #5
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,395

    Re: creating and accessing a checkbox

    Quote Originally Posted by bkelly View Post
    After creating what I thought was a class for the dialog box, I have searched the entire solution and the only OnInitDialog() is the one for the main dialog. There is none for the new dialog box.
    VS 2008, MFC: add OnInitDialog - how?
    Victor Nijegorodov

  6. #6
    Join Date
    Nov 2011
    Posts
    72

    Re: creating and accessing a checkbox

    In the class I just declared the method OnInitDialog() and in there I wrote:
    Code:
    CButton *m_ctl_Check = (CButton*) GetDlgItem( IDC_CBOX_ENABLE_ALL_LOGGING );
    int status = m_ctlCheck->GetCheck();
    m_ctlCheck->SetCheck( BST_CHECKED )
    And it does leave the checkbox checked or uncheck per the argument.
    I found the line of code for GetDlgItem in another thread. Is that the right way here?

    In the Horton book, he indicates I can select the dialog box, right click it, and the popup contains an option to override the OnInitDialog(). I cannot find that dialog. Any instructions on how to find it.

    None the less, I do have a method of getting to the checkboxes now.
    Thanks for your time.

  7. #7
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,395

    Re: creating and accessing a checkbox

    Quote Originally Posted by bkelly View Post
    In the class I just declared the method OnInitDialog()
    You should not "just declare", you must override the OnInitDialog() method! See the link I posted in my previous reply.
    Victor Nijegorodov

  8. #8
    Join Date
    Nov 2011
    Posts
    72

    Re: creating and accessing a checkbox

    Hello,
    In that link I find:
    1.Open the file MyDialog.h.
    2.Select the Properties window.
    3.Place the cursor on the line that reads class CMyDialog : public CDialog. (The top of the Properties window should show "CMyDialog VCCodeClass" - this is important, because the Properties window is highly context sensitive, and you get different options depending on the location of the cursor in the editor.)
    At step 2 I ask: What Properties Window? If I right click the class name itself, there is no Properties selection. If I go to the Visual Studio tool bar and select the button Properties I don't find anything that looks like: class CMyDialog : public CDialog.

    Please provide a bit more detail for me.
    Thank you.

  9. #9
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: creating and accessing a checkbox

    Class View/right click on the class name. The last item on the pop up menu is properties when I do it. Or you can go to the resource editor and right click in the dialog and get a menu with the properties selection too.

  10. #10
    Join Date
    Nov 2011
    Posts
    72

    Re: creating and accessing a checkbox

    I did not know to go there. Now I am a step further down the path and am documenting what I am doing so when I forget, I can figure it out again.

    Thank you for taking the time to reply.
    Last edited by bkelly; August 20th, 2012 at 03:12 PM. Reason: typo

Tags for this Thread

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