Click to See Complete Forum and Search --> : Hide a column in a listctrl programmatically?


Shine B.
May 5th, 1999, 11:35 AM
Is it possible to hide programmatically a column in a listctrl ?
T.I.A.

Shawn Fessenden
May 5th, 1999, 01:51 PM
CListCtrl::SetColumnWidth(nCol, 0);

"Geek used to be a four-letter word. Now it's a six-figure one."

Xiaojian Liu
May 14th, 1999, 09:35 AM
I got a SDI application with listView. I can load
MS-Access files and view any recordset of
database. Sometime I need to insert a new column,
while sometime I need to delete some columns.
I worked around for hours and hours and could
not found the olution: how to delete columns
from the a listview? I wish someone can give
me a clue.

Thanks.

Jim Watters
May 19th, 1999, 08:17 PM
There may be an easier was to get the column R click but I do not know. Let me see if I can extract what I did.. This should get you going. A right click on the column displays a popup menu to either delete or insert a new column.


#define MK_HEADER 0x1000


BEGIN_MESSAGE_MAP(CView_List, CView)
//{{AFX_MSG_MAP(CView_List)
ON_WM_RBUTTONDOWN()
ON_COMMAND(ID_COLUMN_DELETE, OnColumnDelete)
ON_UPDATE_COMMAND_UI(ID_COLUMN_DELETE, OnUpdateColumnDelete)
ON_COMMAND_RANGE(ID_COLUMN_INSERT0, ID_COLUMN_INSERT9, OnColumnInsert)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

// need to make a continuious list of resources ID_COLUMN_INSERT0- ID_COLUMN_INSERT9
void CView_List::OnRButtonDown(UINT nFlags, CPoint point)
{
if(GetFocus() == this && (nFlags & MK_HEADER) == MK_HEADER) //mouse was Over header
{
m_CurColumn = nFlags & 0x0fff;

CMenu aMenu;
CMenu bMenu;
int NumOfDeletedCol = anum;//max of 10
int ColNum;

bMenu.CreatePopupMenu();
for(ColNum=0; ColNum< NumOfDeletedCol; ColNum++)
{
bMenu.AppendMenu(MF_STRING, ID_COLUMN_INSERT0 + ColNum, m_gListColumn[ColNum].pszText);
}

aMenu.CreatePopupMenu();
aMenu.AppendMenu(MF_STRING|MF_POPUP|(ColNum? MF_ENABLED:MF_GRAYED), (UINT)bMenu.m_hMenu, "Insert");
aMenu.AppendMenu(MF_STRING, ID_COLUMN_DELETE, "Delete");

aMenu.TrackPopupMenu(TPM_CENTERALIGN|TPM_RIGHTBUTTON, point.x, point.y, AfxGetMainWnd());
aMenu.DestroyMenu();
}
}
else
CView::OnRButtonDown(nFlags, point);
}



void CView_List::OnColumnDelete()
{
//make sure m_CurColumn is valid
m_MyListCtrl.DeleteColumn(m_CurColumn);
}

void CView_List::OnColumnInsert(UINT nID)
{
m_MyListCtrl.InsertColumn(m_CurColumn, &m_gListColumn[nID- ID_COLUMN_INSERT0]);
}



void CMyHeaderCtrl::OnRButtonDown(UINT nFlags, CPoint point)
{
// Determine the column
HD_HITTESTINFO hd_hittestinfo;
hd_hittestinfo.pt = point;

SendMessage(HDM_HITTEST, 0, (LPARAM)(&hd_hittestinfo));
if(hd_hittestinfo.flags == HHT_ONHEADER || hd_hittestinfo.flags == HHT_NOWHERE)
{
ClientToScreen(&point);
LPARAM lParam = point.x + (((DWORD)point.y) << 16);

if(hd_hittestinfo.flags == HHT_NOWHERE)
hd_hittestinfo.iItem = GetItemCount();

GetParent()->SendMessage(WM_RBUTTONDOWN, hd_hittestinfo.iItem | MK_HEADER, lParam);
}
else
CHeaderCtrl::OnRButtonDown(nFlags, point);
}