Error deleting first column in CListCtrl
-
Hi, I am generating a CListCtrl in Report view (MFC). It loads fine and I can reorder columns by dragging the headers. I've made it so clicking a column header deletes the column - again this works fine except for the first (leftmost) column. Here the header is deleted but the data underneath doesn't change - in fact the data from the last column is deleted: Before:
Header1 Header2 Header3
Data1 Data2 Data3
After trying to delete first column:
Header2 Header3
Data1 Data2
Having created the columns to begin with, I'm setting up the rest of the table like this:
void MyClass::InsertItem(int x, int y, CString &str)
{LVITEM lvi; lvi.mask = LVIF\_TEXT; lvi.iItem = y; lvi.iSubItem = x; lvi.pszText = (LPTSTR)(LPCTSTR)(str); int result; if(x == 0) { result = mCSVListCtrl.InsertItem(&lvi); } else { result = mCSVListCtrl.SetItem(&lvi); }
}
which seems to create the table OK. Then when a header is clicked:
void MyClass::OnLvnColumnclickList(NMHDR *pNMHDR, LRESULT *pResult)
{
LPNMLISTVIEW pNMLV = reinterpret_cast(pNMHDR);mCSVListCtrl.DeleteColumn(pNMLV->iSubItem);// This IS getting zero \*pResult = 0;
}
Any ideas?
-
Hi, I am generating a CListCtrl in Report view (MFC). It loads fine and I can reorder columns by dragging the headers. I've made it so clicking a column header deletes the column - again this works fine except for the first (leftmost) column. Here the header is deleted but the data underneath doesn't change - in fact the data from the last column is deleted: Before:
Header1 Header2 Header3
Data1 Data2 Data3
After trying to delete first column:
Header2 Header3
Data1 Data2
Having created the columns to begin with, I'm setting up the rest of the table like this:
void MyClass::InsertItem(int x, int y, CString &str)
{LVITEM lvi; lvi.mask = LVIF\_TEXT; lvi.iItem = y; lvi.iSubItem = x; lvi.pszText = (LPTSTR)(LPCTSTR)(str); int result; if(x == 0) { result = mCSVListCtrl.InsertItem(&lvi); } else { result = mCSVListCtrl.SetItem(&lvi); }
}
which seems to create the table OK. Then when a header is clicked:
void MyClass::OnLvnColumnclickList(NMHDR *pNMHDR, LRESULT *pResult)
{
LPNMLISTVIEW pNMLV = reinterpret_cast(pNMHDR);mCSVListCtrl.DeleteColumn(pNMLV->iSubItem);// This IS getting zero \*pResult = 0;
}
Any ideas?
Either look using the debugger or put in a trace statement but make sure the sub item value is what you expect it to be. That's the only idea I have because I know that calling DeleteColumn(0) works because I do it often. One of the weird things with the list control is you can't set the first column to be right-justified so the work-around is to add one more column than needed, set column 1 to be right-justified, and then delete column 0. BTW - this was true with older versions and I don't know if it is still necessary. I do it anyway because it still works.
-
Hi, I am generating a CListCtrl in Report view (MFC). It loads fine and I can reorder columns by dragging the headers. I've made it so clicking a column header deletes the column - again this works fine except for the first (leftmost) column. Here the header is deleted but the data underneath doesn't change - in fact the data from the last column is deleted: Before:
Header1 Header2 Header3
Data1 Data2 Data3
After trying to delete first column:
Header2 Header3
Data1 Data2
Having created the columns to begin with, I'm setting up the rest of the table like this:
void MyClass::InsertItem(int x, int y, CString &str)
{LVITEM lvi; lvi.mask = LVIF\_TEXT; lvi.iItem = y; lvi.iSubItem = x; lvi.pszText = (LPTSTR)(LPCTSTR)(str); int result; if(x == 0) { result = mCSVListCtrl.InsertItem(&lvi); } else { result = mCSVListCtrl.SetItem(&lvi); }
}
which seems to create the table OK. Then when a header is clicked:
void MyClass::OnLvnColumnclickList(NMHDR *pNMHDR, LRESULT *pResult)
{
LPNMLISTVIEW pNMLV = reinterpret_cast(pNMHDR);mCSVListCtrl.DeleteColumn(pNMLV->iSubItem);// This IS getting zero \*pResult = 0;
}
Any ideas?
What happens if you create a "dummy" column 0 with a width of 0 (or maybe 1)?
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
-
Hi, I am generating a CListCtrl in Report view (MFC). It loads fine and I can reorder columns by dragging the headers. I've made it so clicking a column header deletes the column - again this works fine except for the first (leftmost) column. Here the header is deleted but the data underneath doesn't change - in fact the data from the last column is deleted: Before:
Header1 Header2 Header3
Data1 Data2 Data3
After trying to delete first column:
Header2 Header3
Data1 Data2
Having created the columns to begin with, I'm setting up the rest of the table like this:
void MyClass::InsertItem(int x, int y, CString &str)
{LVITEM lvi; lvi.mask = LVIF\_TEXT; lvi.iItem = y; lvi.iSubItem = x; lvi.pszText = (LPTSTR)(LPCTSTR)(str); int result; if(x == 0) { result = mCSVListCtrl.InsertItem(&lvi); } else { result = mCSVListCtrl.SetItem(&lvi); }
}
which seems to create the table OK. Then when a header is clicked:
void MyClass::OnLvnColumnclickList(NMHDR *pNMHDR, LRESULT *pResult)
{
LPNMLISTVIEW pNMLV = reinterpret_cast(pNMHDR);mCSVListCtrl.DeleteColumn(pNMLV->iSubItem);// This IS getting zero \*pResult = 0;
}
Any ideas?
Not a solution to the problem but a tip. Instead of deleting columns you might set the width to zero. Then you can implement a hide/show column handler (which requires storing the actual or a default width for each column when hiding to be restored when showing it again). Note also that it might be necessary to get the effective column index when the columns has been reordered (
GetColumnOrderArray
). -
Either look using the debugger or put in a trace statement but make sure the sub item value is what you expect it to be. That's the only idea I have because I know that calling DeleteColumn(0) works because I do it often. One of the weird things with the list control is you can't set the first column to be right-justified so the work-around is to add one more column than needed, set column 1 to be right-justified, and then delete column 0. BTW - this was true with older versions and I don't know if it is still necessary. I do it anyway because it still works.
Thanks for the various replies. The debugger doesn't go beyond DeleteColumn() but I can see it's passing zero. Can you see anything odd about the way I'm creating my sub items that could be causing the problem? I've created the columns first then for each item (row) I'm adding subitems left to right. Otherwise I'll have to go for the dummy first column option.
-
Hi, I am generating a CListCtrl in Report view (MFC). It loads fine and I can reorder columns by dragging the headers. I've made it so clicking a column header deletes the column - again this works fine except for the first (leftmost) column. Here the header is deleted but the data underneath doesn't change - in fact the data from the last column is deleted: Before:
Header1 Header2 Header3
Data1 Data2 Data3
After trying to delete first column:
Header2 Header3
Data1 Data2
Having created the columns to begin with, I'm setting up the rest of the table like this:
void MyClass::InsertItem(int x, int y, CString &str)
{LVITEM lvi; lvi.mask = LVIF\_TEXT; lvi.iItem = y; lvi.iSubItem = x; lvi.pszText = (LPTSTR)(LPCTSTR)(str); int result; if(x == 0) { result = mCSVListCtrl.InsertItem(&lvi); } else { result = mCSVListCtrl.SetItem(&lvi); }
}
which seems to create the table OK. Then when a header is clicked:
void MyClass::OnLvnColumnclickList(NMHDR *pNMHDR, LRESULT *pResult)
{
LPNMLISTVIEW pNMLV = reinterpret_cast(pNMHDR);mCSVListCtrl.DeleteColumn(pNMLV->iSubItem);// This IS getting zero \*pResult = 0;
}
Any ideas?
Member 868926 wrote:
when a header is clicked:
void MyClass::OnLvnColumnclickList(NMHDR *pNMHDR, LRESULT *pResult)
{
LPNMLISTVIEW pNMLV = reinterpret_cast<LPNMLISTVIEW>(pNMHDR);
mCSVListCtrl.DeleteColumn(pNMLV->iSubItem);// This IS getting zero
*pResult = 0;
}Any ideas?
Just for a test: what will happen if you call
mCSVListCtrl.DeleteColumn(0);
from anywhere but from this MyClass::OnLvnColumnclickList(...) message handler?
-
Hi, I am generating a CListCtrl in Report view (MFC). It loads fine and I can reorder columns by dragging the headers. I've made it so clicking a column header deletes the column - again this works fine except for the first (leftmost) column. Here the header is deleted but the data underneath doesn't change - in fact the data from the last column is deleted: Before:
Header1 Header2 Header3
Data1 Data2 Data3
After trying to delete first column:
Header2 Header3
Data1 Data2
Having created the columns to begin with, I'm setting up the rest of the table like this:
void MyClass::InsertItem(int x, int y, CString &str)
{LVITEM lvi; lvi.mask = LVIF\_TEXT; lvi.iItem = y; lvi.iSubItem = x; lvi.pszText = (LPTSTR)(LPCTSTR)(str); int result; if(x == 0) { result = mCSVListCtrl.InsertItem(&lvi); } else { result = mCSVListCtrl.SetItem(&lvi); }
}
which seems to create the table OK. Then when a header is clicked:
void MyClass::OnLvnColumnclickList(NMHDR *pNMHDR, LRESULT *pResult)
{
LPNMLISTVIEW pNMLV = reinterpret_cast(pNMHDR);mCSVListCtrl.DeleteColumn(pNMLV->iSubItem);// This IS getting zero \*pResult = 0;
}
Any ideas?
Member 868926 wrote:
Any ideas?
Additional info: [LVM_DELETECOLUMN message](https://msdn.microsoft.com/en-us/library/windows/desktop/bb774894(v=vs.85).aspx)
-
Member 868926 wrote:
when a header is clicked:
void MyClass::OnLvnColumnclickList(NMHDR *pNMHDR, LRESULT *pResult)
{
LPNMLISTVIEW pNMLV = reinterpret_cast<LPNMLISTVIEW>(pNMHDR);
mCSVListCtrl.DeleteColumn(pNMLV->iSubItem);// This IS getting zero
*pResult = 0;
}Any ideas?
Just for a test: what will happen if you call
mCSVListCtrl.DeleteColumn(0);
from anywhere but from this MyClass::OnLvnColumnclickList(...) message handler?
Victor Nijegorodov wrote:
Just for a test: what will happen if you call
mCSVListCtrl.DeleteColumn(0);
from anywhere but from this MyClass::OnLvnColumnclickList(...) message handler?
It's the same. I think I'll have to go for the dummy left column idea some of you have suggested - maybe I'll use it to number the rows which might be quite nice anyway. Thanks again for all your replies.