CListCtrl(数据的插入、删除、查询)

来源:互联网 发布:龙口美工 编辑:程序博客网 时间:2024/04/30 03:42

1.更新控件行列 

   // 删除所有行、列
   m_pListMIS->DeleteAllItems();
   while(m_pListMIS->DeleteColumn(0));

   m_pListMIS->InsertColumn(0, "名称▲", LVCFMT_LEFT, 100);
   m_pListMIS->InsertColumn(1, "编号▲", LVCFMT_LEFT, 200);
   m_pListMIS->InsertColumn(2, "类型▲", LVCFMT_LEFT, 0);

   // 插入数据
   CMatchProp* pMatchProp = m_pDrawCtrl->GetMatchProp();
   for (int i = 0; i < pMatchProp->mc_arr.GetSize(); ++i)
   {
    m_pListMIS->InsertItem(i, pMatchProp->mc_arr[i].mc);
    m_pListMIS->SetItemText(i, 1, pMatchProp->mc_arr[i].bh);
    m_pListMIS->SetItemText(i, 2, pMatchProp->mc_arr[i].lx);

   }

2.查找编号(根据当前选择位置向后查找,查找到末尾就从开始位置查找,直到当前选择位置)

void CMyCtrl::FindList(CString strNameNumber)
{
 // 1 如果内容为空,不查找
 if (strNameNumber.IsEmpty())
  return;

 // 2 根据条件查找、选择数据
 // 本次查询与上次不同
 if (m_strOldNameNumber != strNameNumber)
  m_nFindIndex = -1;

 // 根据当前选择项,确定查找开始位置
 POSITION pos = m_pList->GetFirstSelectedItemPosition();
 if (pos != NULL)
  m_nFindIndex = m_pList->GetNextSelectedItem(pos);

 // 查找位置过了最后位置,从头再来
 if (m_nFindIndex > m_pList->GetItemCount() - 1)
  m_nFindIndex = -1;

 CMatchProp* pMatchProp = m_pCtrl->GetMatchProp();

 // (1) 从标记位的下一个到最后位置查找
 for (int i = m_nFindIndex + 1; i < m_pList->GetItemCount(); ++i)
 {
  // “名称”和“编号”中都未找到对应数据
  CString strName = m_pList->GetItemText(i, 0);
  CString strNumber = m_pList->GetItemText(i, 1);
  if (strName.Find(strNameNumber) == -1 && strNumber.Find(strNameNumber) == -1)
   continue;

  // 取消已选中的内容
  POSITION pos = m_pList->GetFirstSelectedItemPosition();
  while (pos != NULL)
  {
   int nIndex = m_pList->GetNextSelectedItem(pos);
   m_pList->SetItemState(nIndex, 0, LVIS_SELECTED | LVIS_FOCUSED);
  }

  m_pList->SetItemState(i, LVIS_SELECTED | LVIS_FOCUSED, LVIS_SELECTED | LVIS_FOCUSED);
  m_pList->SetFocus();
  m_pList->EnsureVisible(i, FALSE);
  m_nFindIndex = i;
  m_strOldNameNumber = strNameNumber;
  return;
 }

 // (2) 从开始到标记位查找(前面未找到)
 for (int j = 0; j <= m_nFindIndex; ++j)
 {
  // “名称”和“编号”中都未找到对应数据
  CString strName = m_pList->GetItemText(j, 0);
  CString strNumber = m_pList->GetItemText(j, 1);
  if (strName.Find(strNameNumber) == -1 && strNumber.Find(strNameNumber) == -1)
   continue;

  // 取消已选中的内容
  POSITION pos = m_pList->GetFirstSelectedItemPosition();
  while (pos != NULL)
  {
   int nIndex = m_pList->GetNextSelectedItem(pos);
   m_pList->SetItemState(nIndex, 0, LVIS_SELECTED | LVIS_FOCUSED);
  }

  m_pList->SetItemState(j, LVIS_SELECTED | LVIS_FOCUSED, LVIS_SELECTED | LVIS_FOCUSED);
  m_pList->SetFocus();
  m_pList->EnsureVisible(j, FALSE);
  m_nFindIndex = j;
  m_strOldNameNumber = strNameNumber;
  return;
 }

 MessageBox("未找到!");
}

 

原创粉丝点击