Click here to Skip to main content
15,075,944 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Project: MFC-SDI-C ++ (VS2012)

Hi everybody Im new programming in MFC i have the next:

1. I have built a CListCtrl-looking as a report on a CDialog (The CListCtrl was added using the controls toolbox). The CDialogEx is on a CDockablePanel that are created when the program is opened and always is showed. I added columns and items to CListCtrl to be created without any problems (Class: CCoordsListDlg).

2. I have created a different class that generate double type data (Class: CCoordsGenerator). These data are generated catching the mouse click screen coordinates without any problems. These data should be added on the the CListCtrl as subitems.

3. To add the generate data on the CListCtrl, i have a method that is called from the CCoordsGenerator class but when the method that add the subitems run a DEBUG ASSERTION appears.

4. The DEBUG ASSERTION stop in: ASSERT(::IsWindow(m_hWnd)); of the winctrl2.cpp file in the next section:

C++
BOOL CListCtrl::SetItemText(int nItem, int nSubItem, LPCTSTR lpszText)
{
	ASSERT(::IsWindow(m_hWnd));
	ASSERT((GetStyle() & LVS_OWNERDATA)==0);
	LVITEM lvi;
	lvi.iSubItem = nSubItem;
	lvi.pszText = (LPTSTR) lpszText;
	return (BOOL) ::SendMessage(m_hWnd, LVM_SETITEMTEXT, nItem, (LPARAM)&lvi);
}


The class code where the CListCtrl is created is the next (CCoordsListDlg.cpp):
C++
BOOL CCoordsListDlg::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	// TODO:  Add extra initialization here

	CListCtrl m_coordslist;
    m_coordslist.SetExtendedStyle(LVS_EX_FULLROWSELECT | LVS_EX_SINGLEROW  |LVS_EX_GRIDLINES|LVS_SHOWSELALWAYS);

	// The tree columns are created
	// The column where the points will be displayed is created
	m_coordslist.InsertColumn(0,"point");
	m_coordslist.SetColumnWidth(0,90);
	
	// The column where the point x-coord will be displayed is created
	m_coordslist.InsertColumn(1,"x coord");
	m_coordslist.SetColumnWidth(1,64);
	
	// The column where the point y-coord will be displayed is created
	m_coordslist.InsertColumn(2,"y coord");
	m_coordslist.SetColumnWidth(2,64);
        
    // The points are added
    m_coordslist.InsertItem(0,"p1");
    m_coordslist.InsertItem(1,"p2");
    m_coordslist.InsertItem(2,"p3");
    m_coordslist.InsertItem(3,"p4");
	
	return TRUE; 
	// EXCEPTION: OCX Property Pages should return FALSE
}

A portion of the code where the data are generated is (CCoordsGenerator.cpp):

C++
void CCoordsGenerator::OnLeftButtonDown()
{
    // Get the pickposition
	double q[2];
	picker->GetPickPosition(q);

	// Share pick position
	x_coord = q[0];
	y_coord = q[1];
	
	// The coordinates value are added as subitems to a CListCtrl
	CCoordsListDlg m_coordDlg;
	m_coordDlg.RWImageInteractor(row, x_coord, y_coord); //<<<< HERE IS WHEN THE COORDS ARE SENDED TO A CListCtrl >>>>
	
	// Draw a sphere on the point
	createSpherePoint(row, x_coord,y_coord);
}


The method of the class CCoordsListDlg that add the subitems is:

C++
void CCoordsListDlg::RWImageInteractor(int row, double x_coord, double y_coord)
{
    // Converting the double variable to LPTSTR
    char x_buff[8],
         y_buff[8];
    _gcvt(x_coord,8,x_buf);
    _gcvt(y_coord,8,y_buff);

    // The coordinates are added to list as subitems
	m_coordslist.SetItemText(row,1,x_buff);
	m_coordslist.SetItemText(row,2,y_buff);
}


Could you help me to understand where my error is?
Thank you all so much for your help
Regards

What I have tried:

really I don´t know what is the trouble with my code. I've tried to use the LVITEM methosd to add the subitems and put progamatically the focus on the CListCtrl but the assertion always appears.
Posted
Updated 5-Jun-16 21:38pm
v4
Comments
George Jonsson 5-Jun-16 18:44pm
   
What is the value of m_hWnd?
siqueirosjorge 5-Jun-16 19:32pm
   
Hi George, i have added a Watch on the m_hWnd, the result when the Assertion appear is 0x000000{unused=???}-unable to read memory - Thanks for your comment.
barneyman 5-Jun-16 19:54pm
   
i don't see anywhere where you're creating, or attaching to, that list control (which is LOCAL to the OnInit... it's obscuring the global)
siqueirosjorge 5-Jun-16 20:30pm
   
Hi Barneyman thanks for your comment, i´ve attempted the Georges Jonsson comment. I have changed the code where the CListCtl extended style is defined by the function CreateEx that is where the CListCtl is created programatically but the assertion continues appearsing
barneyman 5-Jun-16 20:55pm
   
because you're still messing with the local decl (of m_coordslist inside OnInit..) and not the member
Philippe Mori 6-Jun-16 18:08pm
   
Item text is probably called too early before the control is created.

The list control instance must be a member of your dialog class. Actually you are creating a new local instance in OnInitDialog using that instead of the class wide member.

A typical implementation would look like:
C++
// CCoordsListDlg.h

class CCoordsListDlg : public CDialogEx
{
// ...
protected:
    CListCtrl m_coordslist;
// ...
};


C++
// CCoordsListDlg.cpp

void CCoordsListDlg::DoDataExchange(CDataExchange* pDX)
{
    CDialogEx::DoDataExchange(pDX);
    // When list is defined by dialog resource template.
    // If not, it must be created / sized in OnInitDialog.
    DDX_Control(pDX, IDC_OF_LIST, m_coordslist);
    // ...
}

BOOL CCoordsListDlg::OnInitDialog()
{
    CDialogEx::OnInitDialog();
 
    // This would create a new local instance. Remove it.
    //CListCtrl m_coordslist;
    m_coordslist.SetExtendedStyle(LVS_EX_FULLROWSELECT | LVS_EX_SINGLEROW  |LVS_EX_GRIDLINES|LVS_SHOWSELALWAYS);
// ...
}


Similar may apply here where a new local CCoordsListDlg is created but not shown:
void CCoordsGenerator::OnLeftButtonDown()
{
    // ...
	
    // This looks suspicious too.
    // Remove it if m_coordDlg is a class member and dialog already shown
    //  or show dialog here.
    CCoordsListDlg m_coordDlg;
    m_coordDlg.RWImageInteractor(row, x_coord, y_coord); //<<<< HERE IS WHEN THE COORDS ARE SENDED TO A CListCtrl >>>>
    
    // ...
}
   
I cannot be entirely sure, because I haven't made any MFC projects in 15 years or so, but I think you need to call the Create method in order to set the parent window.

See CListCtrl::Create[^]
or maybe CListCtrl::CreateEx[^]

This code
C++
CListCtrl m_coordslist;
m_coordslist.SetExtendedStyle(LVS_EX_FULLROWSELECT | LVS_EX_SINGLEROW  |LVS_EX_GRIDLINES|LVS_SHOWSELALWAYS);

should be changed to something like
C++
CListCtrl m_coordslist;
ASSERT(m_coordslist.CreateEx(
    LVS_EX_FULLROWSELECT | LVS_EX_SINGLEROW  |LVS_EX_GRIDLINES|LVS_SHOWSELALWAYS,
    WS_CHILD|WS_VISIBLE|WS_BORDER|LVS_REPORT|LVS_EDITLABELS,
    CRect(10,10,400,200), pParentWnd, IDD_SOME_ID)); 

Where pParentWnd is an instance of CCoordsListDlg and IDD_SOME_ID is the ID of your list control.
   
Comments
siqueirosjorge 5-Jun-16 20:23pm
   
Thanks a lot for your comment George, I've changed the code that you suggest. However the assertion continues appearsing.
George Jonsson 5-Jun-16 20:39pm
   
Then you have to dig into the code with the debugger and try to find out why m_hWnd is NULL.
siqueirosjorge 5-Jun-16 20:52pm
   
ok. i'll start to dig into the code. Thanks for your help George. Regards

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