那么如何判断一个item是否被选中?
5 个解决方案
#1
CListCtrl* pListCtrl = (CListCtrl*) GetDlgItem(IDC_YOURLISTCONTROL);
ASSERT(pListCtrl != NULL);
POSITION pos = pList->GetFirstSelectedItemPosition();
if (pos == NULL)
TRACE0("No items were selected!\n");
else
{
while (pos)
{
int nItem = pList->GetNextSelectedItem(pos);
TRACE1("Item %d was selected!\n", nItem);
// you could do your own processing on nItem here
}
}
ASSERT(pListCtrl != NULL);
POSITION pos = pList->GetFirstSelectedItemPosition();
if (pos == NULL)
TRACE0("No items were selected!\n");
else
{
while (pos)
{
int nItem = pList->GetNextSelectedItem(pos);
TRACE1("Item %d was selected!\n", nItem);
// you could do your own processing on nItem here
}
}
#2
来晚了,搂上的方法可行。
#3
BOOL CYOUDlg::IfItemSelected(nlist)
{
CListCtrl* pListCtrl = (CListCtrl*) GetDlgItem(IDC_YOURLISTCONTROL);
ASSERT(pListCtrl != NULL);
CString strSrcText = pListCtrl->GetItemText(nlist,0);
POSITION pos = pListCtrl->GetFirstSelectedItemPosition();
while (pos)
{
int nItem = pListCtrl->GetNextSelectedItem(pos);
CString strDestText = pListCtrl->GetItemText(nItem,0);
if(strSrcText.Compare(strDestText ) == 0)
return TRUE;
}
return FALSE
}
{
CListCtrl* pListCtrl = (CListCtrl*) GetDlgItem(IDC_YOURLISTCONTROL);
ASSERT(pListCtrl != NULL);
CString strSrcText = pListCtrl->GetItemText(nlist,0);
POSITION pos = pListCtrl->GetFirstSelectedItemPosition();
while (pos)
{
int nItem = pListCtrl->GetNextSelectedItem(pos);
CString strDestText = pListCtrl->GetItemText(nItem,0);
if(strSrcText.Compare(strDestText ) == 0)
return TRUE;
}
return FALSE
}
#4
可是我想对选中的item作一些操作,比如按一个按钮把选中的都delete掉,就不行了。每次只能delte掉一个或两个,大概因为我在点选按钮的时候,那些被选中又被取消了。这个问题怎么解决?
#5
你可以这样,把TreeCtrl加上Check boxes属性,然后用
HTREEITEM hItem = pmyTreeCtrl->GetFirstVisibleItem();
while (hItem != NULL)
{
if(pmyTreeCtrl->GetCheck(hItem))
{
DeleteItem(hItem);
}
hItem = pmyTreeCtrl->GetNextVisibleItem(hItem);
}
HTREEITEM hItem = pmyTreeCtrl->GetFirstVisibleItem();
while (hItem != NULL)
{
if(pmyTreeCtrl->GetCheck(hItem))
{
DeleteItem(hItem);
}
hItem = pmyTreeCtrl->GetNextVisibleItem(hItem);
}
#1
CListCtrl* pListCtrl = (CListCtrl*) GetDlgItem(IDC_YOURLISTCONTROL);
ASSERT(pListCtrl != NULL);
POSITION pos = pList->GetFirstSelectedItemPosition();
if (pos == NULL)
TRACE0("No items were selected!\n");
else
{
while (pos)
{
int nItem = pList->GetNextSelectedItem(pos);
TRACE1("Item %d was selected!\n", nItem);
// you could do your own processing on nItem here
}
}
ASSERT(pListCtrl != NULL);
POSITION pos = pList->GetFirstSelectedItemPosition();
if (pos == NULL)
TRACE0("No items were selected!\n");
else
{
while (pos)
{
int nItem = pList->GetNextSelectedItem(pos);
TRACE1("Item %d was selected!\n", nItem);
// you could do your own processing on nItem here
}
}
#2
来晚了,搂上的方法可行。
#3
BOOL CYOUDlg::IfItemSelected(nlist)
{
CListCtrl* pListCtrl = (CListCtrl*) GetDlgItem(IDC_YOURLISTCONTROL);
ASSERT(pListCtrl != NULL);
CString strSrcText = pListCtrl->GetItemText(nlist,0);
POSITION pos = pListCtrl->GetFirstSelectedItemPosition();
while (pos)
{
int nItem = pListCtrl->GetNextSelectedItem(pos);
CString strDestText = pListCtrl->GetItemText(nItem,0);
if(strSrcText.Compare(strDestText ) == 0)
return TRUE;
}
return FALSE
}
{
CListCtrl* pListCtrl = (CListCtrl*) GetDlgItem(IDC_YOURLISTCONTROL);
ASSERT(pListCtrl != NULL);
CString strSrcText = pListCtrl->GetItemText(nlist,0);
POSITION pos = pListCtrl->GetFirstSelectedItemPosition();
while (pos)
{
int nItem = pListCtrl->GetNextSelectedItem(pos);
CString strDestText = pListCtrl->GetItemText(nItem,0);
if(strSrcText.Compare(strDestText ) == 0)
return TRUE;
}
return FALSE
}
#4
可是我想对选中的item作一些操作,比如按一个按钮把选中的都delete掉,就不行了。每次只能delte掉一个或两个,大概因为我在点选按钮的时候,那些被选中又被取消了。这个问题怎么解决?
#5
你可以这样,把TreeCtrl加上Check boxes属性,然后用
HTREEITEM hItem = pmyTreeCtrl->GetFirstVisibleItem();
while (hItem != NULL)
{
if(pmyTreeCtrl->GetCheck(hItem))
{
DeleteItem(hItem);
}
hItem = pmyTreeCtrl->GetNextVisibleItem(hItem);
}
HTREEITEM hItem = pmyTreeCtrl->GetFirstVisibleItem();
while (hItem != NULL)
{
if(pmyTreeCtrl->GetCheck(hItem))
{
DeleteItem(hItem);
}
hItem = pmyTreeCtrl->GetNextVisibleItem(hItem);
}