您的位置:首页 > 产品设计 > UI/UE

How to select CListCtrl item question

2008-09-19 19:39 441 查看
http://www.codeguru.com/forum/archive/index.php/t-221250.html

Ok, I searched the archives and have asked this question before with no response so I am asking it again. I am still trying to select an item in the CListCtrl. Current I have the following code:

CListCtrl = *m_pPlist = (CListCtrl *)GetDlgItem(IDC_LISTCTRL);
POSITION pos = m_pPList->GetFirstSelectedItemPosition();

if(pos == NULL)
AfxMessageBox("No Items Listed");
return;
...

If there are 5 listed items and none is selected pos is always NULL. If I select one of the items with the mouse then it works. I want just want to click on a button, which I have created, to begin processing the items in the list. I need to automatically go to the first item on the list and then select it.

Can't find out how to set the selected item to the first postion automatically without having to use the mouse to select the item.

Any help will be appreciated.
Mike@spb

John E
December 3rd, 2002, 11:30 AM
I've never actually done this but from the documentation it looks as though CListCtrl::GetItemState() and CListCtrl::SetItemState() are intended for this purpose. They allow you to set an item's state (for example to be LVIS_SELECTED) by knowing it's index.

You can also change an item's position by using CListCtrl::GetItemPosition() and CListCtrl::SetItemPosition() - again by using the item's index.

Elrond
December 3rd, 2002, 11:38 AM
John E is right.

To select an item you must use:

m_ListCtrl.SetItemState(i, LVIS_SELECTED, LVIS_SELECTED);

To unselect an item you must use:

m_ListCtrl.SetItemState(i, 0, LVIS_SELECTED);

The function you were using (GetFirstSelectedItemPosition) is made to GET the selected items, not to SET them.

In your case, after clicking an item, this function would only return you the index of this item. No way you can process all the items that way.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: