CListCtrl(數據的插入、刪除、查詢)

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("未找到!");
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章