15 мая 2023 года "Исходники.РУ" отмечают своё 23-летие!
Поздравляем всех причастных и неравнодушных с этим событием!
И огромное спасибо всем, кто был и остаётся с нами все эти годы!

Главная Форум Журнал Wiki DRKB Discuz!ML Помощь проекту


Derived Combo Box

Raja Bhattacharjee -- raja@bhi.com
Tuesday, March 11, 1997

Environment: VC++ 4.2-flat, NT 4.0

I have derived a class MyComboBox from CComboBox to handle
SetDataItemPtr
for the items. I am using the combobox in many dialogs where I have to
set
the dataitem value to a CString object.

I have added a method SetCStringItem(index,CString) to MyComboBox
{
	// allocate space in the heap (needs to be destroyed explicitely)
	LPSTR pKeyVal = new TCHAR[pstr.GetLength()+1];
	if (pKeyVal == NULL) return CB_ERR;

	// copy the data to the ptr
	strcpy(pKeyVal,pstr);

	// set the data item
	m_bDataSet = TRUE;
	return (SetItemDataPtr(pIndex, pKeyVal ));
}

Now My problem is I am not finding the correct event to destroy the
items.
I have to use GetCount() and GetItemDataPtr() which
gives Assertion in destructor. I tried DestroyWindow(), OnClose(),
OnDestroy() none of them gets invoked before destructor. Can anyone
suggest me a way to do the cleanup ?




LeRoy Baxter -- lbaxter@transport.com
Friday, March 14, 1997

[Mini-digest: 2 responses]


The MSVC++ help system shows  a CComboBox class member of
"DeleteItem".  Override this function to delete your datapointers.

-----Original Message-----
From:	Raja Bhattacharjee [SMTP:raja@bhi.com]
Sent:	Tuesday, March 11, 1997 2:25 PM
To:	mfc-l@netcom.com
Subject:	Derived Combo Box

Environment: VC++ 4.2-flat, NT 4.0

I have derived a class MyComboBox from CComboBox to handle
SetDataItemPtr
for the items. I am using the combobox in many dialogs where I have to
set
the dataitem value to a CString object.

I have added a method SetCStringItem(index,CString) to MyComboBox
{
	// allocate space in the heap (needs to be destroyed explicitely)
	LPSTR pKeyVal = new TCHAR[pstr.GetLength()+1];
	if (pKeyVal == NULL) return CB_ERR;

	// copy the data to the ptr
	strcpy(pKeyVal,pstr);

	// set the data item
	m_bDataSet = TRUE;
	return (SetItemDataPtr(pIndex, pKeyVal ));
}

Now My problem is I am not finding the correct event to destroy the
items.
I have to use GetCount() and GetItemDataPtr() which
gives Assertion in destructor. I tried DestroyWindow(), OnClose(),
OnDestroy() none of them gets invoked before destructor. Can anyone
suggest me a way to do the cleanup ?
-----From: "Jason Healy" 

I have managed to override the OnDestroy notification function, and
successfully
deleted the data I allocated. You must delete the data allocated before
calling the
base class implementation, which destroys the internal data, and therefor
your link to the
data allocated by you.

void CPtrComboBox::OnDestroy() 
{
	for (int nIndex = 0; nIndex < GetCount(); nIndex++)
		delete[] (LPTSTR)GetItemDataPtr(nIndex);

	CComboBox::OnDestroy();	
}

I ran this using VC 4.2 ont NT 3.51

Jason
Jason@hkjcs.oz.au

----------
> Environment: VC++ 4.2-flat, NT 4.0
> 
> I have derived a class MyComboBox from CComboBox to handle
> SetDataItemPtr
> for the items. I am using the combobox in many dialogs where I have to
> set
> the dataitem value to a CString object.
> 
> I have added a method SetCStringItem(index,CString) to MyComboBox
> {
> 	// allocate space in the heap (needs to be destroyed explicitely)
> 	LPSTR pKeyVal = new TCHAR[pstr.GetLength()+1];
> 	if (pKeyVal == NULL) return CB_ERR;
> 
> 	// copy the data to the ptr
> 	strcpy(pKeyVal,pstr);
> 
> 	// set the data item
> 	m_bDataSet = TRUE;
> 	return (SetItemDataPtr(pIndex, pKeyVal ));
> }
> 
> Now My problem is I am not finding the correct event to destroy the
> items.
> I have to use GetCount() and GetItemDataPtr() which
> gives Assertion in destructor. I tried DestroyWindow(), OnClose(),
> OnDestroy() none of them gets invoked before destructor. Can anyone
> suggest me a way to do the cleanup ?
> 





Jason Healy -- jason@hkjcs.oz.au
Wednesday, March 19, 1997

DeleteItem is only called for an owner-draw Combobox
Jason
jason@hkjcs.oz.au

----------
> 
> [Mini-digest: 2 responses]
> 
> 
> The MSVC++ help system shows  a CComboBox class member of
> "DeleteItem".  Override this function to delete your datapointers.
> 
> -----Original Message-----
> From:	Raja Bhattacharjee [SMTP:raja@bhi.com]
> Sent:	Tuesday, March 11, 1997 2:25 PM
> To:	mfc-l@netcom.com
> Subject:	Derived Combo Box
> 
> Environment: VC++ 4.2-flat, NT 4.0
> 
> I have derived a class MyComboBox from CComboBox to handle
> SetDataItemPtr
> for the items. I am using the combobox in many dialogs where I have to
> set
> the dataitem value to a CString object.
> 
> I have added a method SetCStringItem(index,CString) to MyComboBox
> {
> 	// allocate space in the heap (needs to be destroyed explicitely)
> 	LPSTR pKeyVal = new TCHAR[pstr.GetLength()+1];
> 	if (pKeyVal == NULL) return CB_ERR;
> 
> 	// copy the data to the ptr
> 	strcpy(pKeyVal,pstr);
> 
> 	// set the data item
> 	m_bDataSet = TRUE;
> 	return (SetItemDataPtr(pIndex, pKeyVal ));
> }
> 
> Now My problem is I am not finding the correct event to destroy the
> items.
> I have to use GetCount() and GetItemDataPtr() which
> gives Assertion in destructor. I tried DestroyWindow(), OnClose(),
> OnDestroy() none of them gets invoked before destructor. Can anyone
> suggest me a way to do the cleanup ?
> -----From: "Jason Healy" 
> 
> I have managed to override the OnDestroy notification function, and
> successfully
> deleted the data I allocated. You must delete the data allocated before
> calling the
> base class implementation, which destroys the internal data, and therefor
> your link to the
> data allocated by you.
> 
> void CPtrComboBox::OnDestroy() 
> {
> 	for (int nIndex = 0; nIndex < GetCount(); nIndex++)
> 		delete[] (LPTSTR)GetItemDataPtr(nIndex);
> 
> 	CComboBox::OnDestroy();	
> }
> 
> I ran this using VC 4.2 ont NT 3.51
> 
> Jason
> Jason@hkjcs.oz.au
> 
> ----------
> > Environment: VC++ 4.2-flat, NT 4.0
> > 
> > I have derived a class MyComboBox from CComboBox to handle
> > SetDataItemPtr
> > for the items. I am using the combobox in many dialogs where I have to
> > set
> > the dataitem value to a CString object.
> > 
> > I have added a method SetCStringItem(index,CString) to MyComboBox
> > {
> > 	// allocate space in the heap (needs to be destroyed explicitely)
> > 	LPSTR pKeyVal = new TCHAR[pstr.GetLength()+1];
> > 	if (pKeyVal == NULL) return CB_ERR;
> > 
> > 	// copy the data to the ptr
> > 	strcpy(pKeyVal,pstr);
> > 
> > 	// set the data item
> > 	m_bDataSet = TRUE;
> > 	return (SetItemDataPtr(pIndex, pKeyVal ));
> > }
> > 
> > Now My problem is I am not finding the correct event to destroy the
> > items.
> > I have to use GetCount() and GetItemDataPtr() which
> > gives Assertion in destructor. I tried DestroyWindow(), OnClose(),
> > OnDestroy() none of them gets invoked before destructor. Can anyone
> > suggest me a way to do the cleanup ?
> > 
> 
> 





DFPav@aol.com
Monday, March 24, 1997

All:
I would guess that listbox and combo box details are the same, but I am not
sure.  For listboxes, however I know that:
they must be owner draw to receive WM_DRAWITEM (obvious)
but they must also NOT have HASSTRINGS to receive WM_COMPAREITEM,
WM_DELETEITEM
Hope this helps,
Dan




Become an MFC-L member | Вернуться в корень Архива |