CString strText;
int nColumnCount = m_myListCtrl.GetHeaderCtrl()->GetItemCount();// Insert 10 items in the list view control.
for (int i = 0; i < 10; i++)
{strText.Format(TEXT("item %d"), i);// Insert the item, select every other item.m_myListCtrl.InsertItem(LVIF_TEXT | LVIF_STATE, i, strText, (i % 2) == 0 ? LVIS_SELECTED : 0, LVIS_SELECTED, 0, 0);// Initialize the text of the subitems.for (int j = 1; j < nColumnCount; j++){strText.Format(TEXT("sub-item %d %d"), i, j);m_myListCtrl.SetItemText(i, j, strText);}
}
4 一直选中一行
在默认情况下,当选一行,再点击其它地方时,原先选中的一行会推动高亮显示,如果你想保留高亮显示,则可以在属性面板上找到属性Always Show Selection,并设置为true.
// Set the selection mark to the first item only if no other item is
// selected.
if (m_myListCtrl.GetSelectionMark() == -1)
m_myListCtrl.SetSelectionMark(0);
// Set the selection mark to the first item only if no other item is
// selected.
if (m_myListCtrl.GetSelectionMark() == -1)m_myListCtrl.SetSelectionMark(0);
POSITION pos = m_list.GetFirstSelectedItemPosition();
if (pos == NULL)
TRACE0("No items were selected!\n");
else
{
while (pos)
{
int nItem = m_list.GetNextSelectedItem(pos);
TRACE1("Item %d was selected!\n", nItem);
// you could do your own processing on nItem here
}
}
POSITION pos = m_list.GetFirstSelectedItemPosition();
if (pos == NULL)TRACE0("No items were selected!\n");
else
{while (pos){int nItem = m_list.GetNextSelectedItem(pos);TRACE1("Item %d was selected!\n", nItem);// you could do your own processing on nItem here}
}
获取单选项的示例代码:
[cpp] view plaincopy print?
POSITION pos =m_CLC_Record.GetFirstSelectedItemPosition();
if (pos == NULL)
{
AfxMessageBox("请先选择一项记录!");
return;
}
else
{
int nSel =m_CLC_Record.GetNextSelectedItem(pos);
//do something
}
POSITION pos =m_CLC_Record.GetFirstSelectedItemPosition();
if (pos == NULL)
{AfxMessageBox("请先选择一项记录!");return;
}
else
{int nSel =m_CLC_Record.GetNextSelectedItem(pos);//do something
}
7 绑定数据
[cpp] view plaincopy print?
// If any item's data is equal to zero then reset it to -1.
for (int i=0; i < m_myListCtrl.GetItemCount(); i++)
{
if (m_myListCtrl.GetItemData(i) == 0)
{
m_myListCtrl.SetItemData(i, (DWORD) -1);
}
}
// If any item's data is equal to zero then reset it to -1.
for (int i=0; i < m_myListCtrl.GetItemCount(); i++)
{if (m_myListCtrl.GetItemData(i) == 0){m_myListCtrl.SetItemData(i, (DWORD) -1);}
}