Click here to Skip to main content
15,897,371 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi everyone there,

I am developing a dialog based application in MFC and I have a CListCtrl with checkboxes. Code is as follows:
CListCtrl m_gridOutput;
m_gridOutput.SetExtendedStyle(m_gridCtrlEx.GetExtendedStyle() | LVS_EX_CHECKBOXES);

I can track the changes in the ListControl but now I am trying to change the status of the Checkbox of an item. Unfortunately I am getting Assertion errors, when I try to use the following piece of code.
m_gridOutput.SetItem(1,0,LVIF_STATE,NULL,0,dwFlags,dwFlags,0);
(or)
ListView_SetItemState(m_gridOutput.GetSafeHwnd(),VarId,INDEXTOSTATEIMAGEMASK((fCheck)+ 1), LVIS_STATEIMAGEMASK);
(or)
ListView_SetCheckState((m_gridOutput.GetSafeHwnd(),Id,TRUE);


I want to add one more thing here. If i use SetCheck (or) ListView_SetCheckState in OnInitDialog(), it works fine. But if i try to use it in a seperate function, it gives assertion error. How to handle this?.

Any help is greatly appreciated.

Shiva
Posted
Updated 6-Apr-11 18:54pm
v5

According to MSDN[^] you should use the CListCtrl::SetCheck() method.
 
Share this answer
 
What does the assertion say? If you break into the code (with the Retry button) to see where it asserted, you usually have comments to help understand the problem.

Check that the item index is correct.
Check that GetSafeHwnd doesn't return NULL. If the return value is NULL, then it means the control is not fully created yet (override OnInitDialog and put your code there).
 
Share this answer
 
OnInitDialog() is a method that is called immediately after the creation of the parent CWnd and all of its children. That's usually the place where you want to do initialization of all GUI components (or even any point after that would be valid, but that's where the framework specifies you should do it).

When you say that it causes an assert in any other function, well... it will cause an error if that particular window hasn't been created when you called that function (such as in a constructor), but not if the window has already been created (such as in an event handler that will only be called post window creation).
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900