How to select CListCtrl item question

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.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值