Almost every one of us who are programming in VC++ ,
will come across the List control. There are many cases where there is a
need to represent data in List Control in multiple columns. By default it
is not possible to modify the data in the List control itself.
In this small article I am putting a simple way to edit any value in any
column in a Report style List control. The logic here is simple, whenever
user clicks on an sub-item which he wants to modify at that place I am
displaying a edit box and allowing to modify the value. Once modified and by
clicking the ENTER key, the updated value is set in the List control. Here I am
assuming the user is familiar with VC++ and using Class Wizard
- Using MFC AppWizard, create a Dialog Based application.
Give the application name as MultipleColumns. By default the wizard
adds OK and Cancel buttons to the Dialog, Remove these two
buttons.
- Now Add a List-Control and in properties change the
style to Report, this style is necessary if we want multiple columns
- Add two buttons to the Dialog and name them as OK and Exit
- Add one Edit box and in the properties remove the Border style
- Using the Class Wizard add the message handlers for the OK and Exit Buttons.
Add the following code to those functions
void CMultipleColumnsDlg::OK()
{
CDialog::EndDialog (0);
}
void CMultipleColumnsDlg::OnExit()
{
CDialog::EndDialog (0);
}
- Add a function called
InsertItems()
to the CMulipleColumnsDlg
class.
void InsertItems();
In the function handler add the following code
void CMultipleColumnsDlg::InsertItems()
{
HWND hWnd = ::GetDlgItem(m_hWnd, IDC_LIST1);
LVCOLUMN list;
list.mask = LVCF_TEXT |LVCF_WIDTH|
LVCF_FMT |LVCF_SUBITEM;
list.fmt = LVCFMT_LEFT;
list.cx = 50;
list.pszText = "S.No";
list.iSubItem = 0;
::SendMessage(hWnd,LVM_INSERTCOLUMN,
(WPARAM)0,(WPARAM)&list);
list.cx = 100;
list.pszText = "Name";
list.iSubItem = 1;
::SendMessage(hWnd ,LVM_INSERTCOLUMN,
(WPARAM)1,(WPARAM)&list);
list.cx = 100;
list.pszText = "Address";
list.iSubItem = 2;
::SendMessage(hWnd ,LVM_INSERTCOLUMN,
(WPARAM)1,(WPARAM)&list);
list.cx = 100;
list.pszText = "Country";
list.iSubItem = 2;
::SendMessage(hWnd ,LVM_INSERTCOLUMN,
(WPARAM)1,(WPARAM)&list);
SetCell(hWnd,"1",0,0);
SetCell(hWnd,"Prabhakar",0,1);
SetCell(hWnd,"Hyderabad",0,2);
SetCell(hWnd,"India",0,3);
SetCell(hWnd,"2",1,0);
SetCell(hWnd,"Uday",1,1);
SetCell(hWnd,"Chennai",1,2);
SetCell(hWnd,"India",1,3);
SetCell(hWnd,"3",2,0);
SetCell(hWnd,"Saradhi",2,1);
SetCell(hWnd,"Bangolore",2,2);
SetCell(hWnd,"India",2,3);
SetCell(hWnd,"4",3,0);
SetCell(hWnd,"Surya",3,1);
SetCell(hWnd,"Calcutta",3,2);
SetCell(hWnd,"India",3,3);
}
- Add another function called
SetCell( )
to the CMultipleColumnsDlg
class
void SetCell(HWND hWnd1, CString value, int nRow, int nCol);
In the function handler add the following code
void CMultipleColumnsDlg::SetCell(HWND hWnd1,
CString value, int nRow, int nCol)
{
TCHAR szString [256];
wsprintf(szString,value ,0);
LVITEM lvItem;
lvItem.mask = LVIF_TEXT;
lvItem.iItem = nRow;
lvItem.pszText = szString;
lvItem.iSubItem = nCol;
if(nCol >0)
::SendMessage(hWnd1,LVM_SETITEM,
(WPARAM)0,(WPARAM)&lvItem);
else
ListView_InsertItem(hWnd1,&lvItem);
}
- Add one more function called
GetItemText()
to the same Class
CString GetItemText(HWND hWnd, int nItem, int nSubItem) const;
Inside the function add the following code
CString CMultipleColumnsDlg::GetItemText(
HWND hWnd, int nItem, int nSubItem) const
{
LVITEM lvi;
memset(&lvi, 0, sizeof(LVITEM));
lvi.iSubItem = nSubItem;
CString str;
int nLen = 128;
int nRes;
do
{
nLen *= 2;
lvi.cchTextMax = nLen;
lvi.pszText = str.GetBufferSetLength(nLen);
nRes = (int)::SendMessage(hWnd,
LVM_GETITEMTEXT, (WPARAM)nItem,
(LPARAM)&lvi);
str.ReleaseBuffer();
return str;
}
- Also add two member variables to the
CMultipleColumnsDlg
class which are of type int
int nItem, nSubItem;
- From the Class wizard add
NM_CLICK
notification to the List control.
Inside the function handler write the following code
void CMultipleColumnsDlg::OnClickList(
NMHDR* pNMHDR, LRESULT* pResult)
{
Invalidate();
HWND hWnd1 = ::GetDlgItem (m_hWnd,IDC_LIST1);
LPNMITEMACTIVATE temp = (LPNMITEMACTIVATE) pNMHDR;
RECT rect;
nItem = temp->iItem;
nSubItem = temp->iSubItem;
if(nSubItem == 0 || nSubItem == -1 || nItem == -1)
return ;
CString str = GetItemText(hWnd1,nItem ,
nSubItem);
RECT rect1,rect2;
ListView_GetSubItemRect(hWnd1,temp->iItem,
temp->iSubItem,LVIR_BOUNDS,&rect);
::GetWindowRect(temp->hdr.hwndFrom,&rect1);
::GetWindowRect(m_hWnd,&rect2);
int x=rect1.left-rect2.left;
int y=rect1.top-rect2.top;
if(nItem != -1)
::SetWindowPos(::GetDlgItem(m_hWnd,IDC_EDIT1),
HWND_TOP,rect.left+x,rect.top+4,
rect.right-rect.left - 3,
rect.bottom-rect.top -1,NULL);
::ShowWindow(::GetDlgItem(m_hWnd,IDC_EDIT1),SW_SHOW);
::SetFocus(::GetDlgItem(m_hWnd,IDC_EDIT1));
::Rectangle(::GetDC(temp->hdr.hwndFrom),
rect.left,rect.top-1,rect.right,rect.bottom);
::SetWindowText(::GetDlgItem(m_hWnd,IDC_EDIT1),str);
*pResult = 0;
}
- To handle the ENTER key we need to write the virtual function
OnOk
in the
MultipleColumnsDlg.h, so add the following as protected member
afx_msg void OnOK();
In MultipleColumnsDlg.cpp write the following code.
void CMultipleColumnsDlg::OnOK()
{
CWnd* pwndCtrl = GetFocus();
int ctrl_ID = pwndCtrl->GetDlgCtrlID();
CString str;
switch (ctrl_ID)
{
case IDC_EDIT1:
GetDlgItemText(IDC_EDIT1,str);
SetCell(::GetDlgItem (m_hWnd,IDC_LIST1),
str,nItem,nSubItem);
::SendDlgItemMessage(m_hWnd,IDC_EDIT1,
WM_KILLFOCUS,0,0);
::ShowWindow(::GetDlgItem(m_hWnd,IDC_EDIT1),
SW_HIDE);
break;
default:
break;
}
}
- The last step in the implementation is add the following code in side the
OnInitDialog
function
ListView_SetExtendedListViewStyle(::GetDlgItem
(m_hWnd,IDC_LIST1),LVS_EX_FULLROWSELECT |
LVS_EX_GRIDLINES);
InsertItems();
::ShowWindow(::GetDlgItem(m_hWnd,IDC_EDIT1),SW_HIDE);
With this I will hope , it will give an idea to edit any sub items in a List control.