Checkbox checked at startup.

Posted on 2002-07-27
Last Modified: 2013-11-20
Hi Guys, in these days I'm working to a gui to some code I wrote.
I've not done it before (not with MSVC++) but seems I?ve done something looking into exemples.
The last thing I must solve is to have a checkbox automaticly checked when I run the program but i did't found any exemple or document on it.
Any idea?

I'd like to set to checked as I set the default value for olter elements like:


something like:


Many thanks to who will answer.

BTW: please don't write strong things since I'm newbie in this and I need this thing just for this interface.

Question by:zuppy
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +2
LVL 32

Accepted Solution

jhance earned 150 total points
ID: 7181813
Assuming you have a checkbox with an ID of IDC_CHECKBOX do in your OnInitDialog:

CButton *pButton = (CButton *)GetDlgItem(IDC_CHECKBOX);

Author Comment

ID: 7181849
I?ve just tried.
It compiles, but when I try to exec it it crashes with an error on MFC42D.DLL.
This is the code I've pasted:

CButton *pButton = (CButton *)GetDlgItem(hDlg,IDC_CHECK2_SMOOTH);


(you forgot the "hDlg" handler)

Any idea why?

LVL 32

Expert Comment

ID: 7181891
I didn't forget anything. You posted this in the MFC topic area.  I guess I (foolishly) assumed this was an MFC program.

I was referring to CWnd::GetDlgItem() and NOT ::GetDlgItem() as you have used.  If you are going to use ::GetDlgItem() then you must pass it the HWND for the dialog containing the checkbox.
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Expert Comment

ID: 7182205
2 things to note:
a.) jhance's suggestion is absolutely correct
b.) you can make it a bit more safe, or at least easier to debug. Use the following code instead:

CButton* pButton = static_cast<CButton*>( GetDlgItem( IDC_CHECK2_SMOOTH ) );
ASSERT( NULL != pButton );

If you call this function at a time where the button hasn't been created you will be notified by a dialog popping up when you are running in debug mode. The ASSERT()-function will not be compiled into the release build.

Expert Comment

ID: 7189466
Hello zuppy

1) Is your ID correctly spelled?
2) does it exist on the dialog or window you are have handle off?
3) If you use GetDlgItem with 2 parameters, what you
get back will be HWND, so you can never cast it into
a CButton! Instead use function SetCheck with 2 parameters
or you might have to use SendMessage function to
change button styles.
4) In any case, it is good idea to check for NULL
return value before using it as shown by fl0yd.


Expert Comment

ID: 7196534
Why don't you add a control to your checkbox using class wizard and then you can intilaize it to checked in OnInitDaialog()


Author Comment

ID: 7196814
Hi guys i've solved using all suggestion given by everybody.
So to who I have to give points? ;-)


Expert Comment

ID: 7199030
Whose answer was closest or contained your
your info?

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question