LPARAM - worked example
key : purple - original code / blue - new code
Step 1 :
Continue with the program names.
Step 2 :
Edit the AddNameToListCtrl() function within the ListCtrl. The LPARAM feature must be activated :
void
CNameListView::AddNameToListCtrl(CName * pNameTemp)
{
CListCtrl& m_listCtrl = GetListCtrl();
LV_ITEM listItem;
listItem.mask = LVIF_PARAM | LVIF_TEXT | LVIF_IMAGE;
listItem.iSubItem = 0;
listItem.lParam = (LPARAM)pNameTemp;
CString* temp = pNameTemp->GetTitleAddress();
Step 3 :
Now using LPARAM members the code in the other functions can be simplified. Edit the DeleteNameFromList() function :
void
CNameListView::DeleteNameFromListCtrl(CObject * pHintTemp)
{
CListCtrl& m_listCtrl = GetListCtrl();
LVFINDINFO findStruct;
findStruct.flags = LVFI_PARAM;
findStruct.lParam = (LPARAM)pHintTemp;
int nTemp = m_listCtrl.FindItem(&findStruct);
m_listCtrl.DeleteItem( m_listCtrl.FindItem(&findStruct) );
m_listCtrl.SetItemState(nTemp, LVIS_SELECTED, LVIS_SELECTED);
}
Step 4 :
The SetNewItem() function may be similarly changed :
void
CNameListView::SelectNewItem(CObject *pHintTemp)
{
CListCtrl& m_listCtrl = GetListCtrl();
LVFINDINFO findStruct;
findStruct.flags = LVFI_PARAM;
findStruct.lParam = (LPARAM)pHintTemp;
m_listCtrl.SetItemState(m_listCtrl.FindItem(&findStruct), LVIS_SELECTED,
LVIS_SELECTED);
}
Step 5 :
Add a function which allows editing of the data in the ListCtrl :
void
CNameListView::EditNameInListCtrl(CObject *pHintTemp)
{
CListCtrl& m_listCtrl = GetListCtrl();
LVFINDINFO findStruct;
findStruct.flags = LVFI_PARAM;
// this is the item that we want to change
findStruct.lParam = (LPARAM)pHintTemp;
// now change the item text - code similar to Initial function
LV_ITEM editItem;
editItem.iItem = m_listCtrl.FindItem(&findStruct);
CName * pNameTemp = (CName*)pHintTemp;
CString* temp = ((pNameTemp->GetTitleAddress()));
LPTSTR pBufTitle = (LPTSTR) temp->GetBuffer(temp->GetLength());
editItem.pszText = pBufTitle;
temp = ((pNameTemp->GetFirstNameAddress()));
LPTSTR pBufFirstName = (LPTSTR) temp->GetBuffer(temp->GetLength());
temp = ((pNameTemp->GetSecondNameAddress()));
LPTSTR pBufSecondName = (LPTSTR) temp->GetBuffer(temp->GetLength());
if( pNameTemp->GetTitle() == "Mr" ||
pNameTemp->GetTitle() == "Sir")
{editItem.iImage = 0;
}
else if( pNameTemp->GetTitle() == "Dr" ||
pNameTemp->GetTitle() == "Prof")
{editItem.iImage = 2;
}
else // 99% certain that they must be female
{editItem.iImage = 1;
}
// editItem.iItem = m_listCtrl.GetItemCount();
m_listCtrl.InsertItem( &editItem );
// changes the text to the edited text
m_listCtrl.SetItemText( editItem.iItem, 0, pBufTitle );
m_listCtrl.SetItemText( editItem.iItem, 1, pBufFirstName );
m_listCtrl.SetItemText( editItem.iItem, 2, pBufSecondName );
temp->ReleaseBuffer();
}
Click here for the basic exercise
Click here for the source code
Click here to return to menu