Solved

Checkbox checked at startup.

Posted on 2002-07-27
10
640 Views
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:

SetDlgItemText(hDlg,IDIPPARTE1,"192");

something like:

"SetDlgItemText(hDlg,IDIPPARTE1,cheched);"

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.

Zuppy
 
0
Comment
Question by:zuppy
  • 2
  • 2
  • 2
  • +2
10 Comments
 
LVL 32

Accepted Solution

by:
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);
pButton->SetCheck(1);
0
 

Author Comment

by:zuppy
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);

pButton->SetCheck(1);

(you forgot the "hDlg" handler)

Any idea why?

Zuppy
0
 
LVL 32

Expert Comment

by:jhance
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.
0
 
LVL 8

Expert Comment

by:fl0yd
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 );
pButton->SetCheck(1);

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.
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 2

Expert Comment

by:SamratAshok
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.

0
 
LVL 1

Expert Comment

by:Mazen
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()


Mazen
0
 

Author Comment

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

Zuppy
0
 
LVL 2

Expert Comment

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

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Windows Drag & Drop Location 2 85
Folder Comparison 12 52
wordmultiple challenge 12 93
Not needed 13 58
Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
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.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

744 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now