-
February 10th, 2006, 07:01 AM
#16
Re: CListCtrl: How to make sure that 1 item is always selected?
Originally Posted by martho
No, because the conditions I ask for (like "how many items are selected") are always the same after I get the "one item has been deselected"-notification, no matter if I select another item or select another. Please tell me the conditions you mean.
Exactly. However, here is what you can do:
Instead of LVN_ITEMCHANGED, handle the NM_CLICK notification (NM_CLICK will only be sent after the selection change is complete). Inside the NM_CLICK handler, you can test whether an item is selected with GetSelectedCount().
Note that the usual problems with NM_CLICK (selection change via keyboard is ignored) do not apply here: You can't use the keyboard to select nothing in a listview control.
-
February 10th, 2006, 07:03 AM
#17
Re: CListCtrl: How to make sure that 1 item is always selected?
Originally Posted by humptydumpty
As i told you if you are going to click on Empty row .this doesn't mean that that row is not selected you have to change the baheviour just use GetItemtext method of ListCtrl and if only this returen nothing you can say yes this is a empty row and you can switch your list control Focus to your Previous Selected item.
Sorry, but that's complete nonsense. The empty area below the items does not contain "empty rows" - there are simply no items you could click, iterate, or get content from.
-
February 10th, 2006, 07:03 AM
#18
Re: CListCtrl: How to make sure that 1 item is always selected?
Originally Posted by humptydumpty
As i told you if you are going to click on Empty row .this doesn't mean that that row is not selected you have to change the baheviour just use GetItemtext method of ListCtrl and if only this returen nothing you can say yes this is a empty row and you can switch your list control Focus to your Previous Selected item.
But imagine you have a sizeable CListCtrl. How many empty rows would you insert then? Should you always delete and insert items on every resize?
And what happens if a user clicks right of the lst coloumn? So you have to insert empty coloums as well. In normal behavior, CListCtrl even deselects a selected item if you click on a subitem > 0 or on a subitem=0, but where the text doesn't fill the whole coloumn.
-
February 10th, 2006, 07:16 AM
#19
Re: CListCtrl: How to make sure that 1 item is always selected?
I found a possible solution:
When an item is deselected, I post a user-defined message to the dialog (parent of the ListCtrl). Because of the PostMessage, the message-function of it is called AFTER all notifications, so I can check here if an item is selected or not.
-
February 10th, 2006, 07:18 AM
#20
Re: CListCtrl: How to make sure that 1 item is always selected?
Originally Posted by martho
I found a possible solution:
When an item is deselected, I post a user-defined message to the dialog (parent of the ListCtrl). Because of the PostMessage, the message-function of it is called AFTER all notifications, so I can check here if an item is selected or not.
Well... Have you missed my post about using NM_CLICK?
Last edited by gstercken; February 10th, 2006 at 07:21 AM.
-
February 10th, 2006, 07:21 AM
#21
Re: CListCtrl: How to make sure that 1 item is always selected?
huh First thing it's depend on the size of ListControl .
Second thing if u click on the Empty row of ListControl any message handler Nm_CliCK or wat ever you use will fire a Event for you.defe your Function will not Succed because you are making a For loop as i said earlier but if you want to perform calculation on the basis of selection only without any look so that time this nonsense will create a sense for you.
Thankyou.
and Plz Check out Once Again for NM_CLICK & item itemchanging Message handler in MSDN
Thanx
-
February 10th, 2006, 07:35 AM
#22
Re: CListCtrl: How to make sure that 1 item is always selected?
Originally Posted by humptydumpty
huh First thing it's depend on the size of ListControl .
Not clear what you mean. Independently of the size of a list view control, the empty area below the items does not contain empty rows.
Originally Posted by humptydumpty
Second thing if u click on the Empty row of ListControl any message handler Nm_CliCK or wat ever you use will fire a Event for you.defe
you.defe? In any case, that's not correct. Not the "message handler will fire an event" - but a notification message will be sent to the control's parent window, and the message handler handles that message.
Originally Posted by humptydumpty
your Function will not Succed because you are making a For loop as i said earlier but if you want to perform calculation on the basis of selection only without any look so that time this nonsense will create a sense for you.
Again, not clear what you mean. The for-loop is not required, simply checking if GetSelectedCount() returns 0 is sufficient to find out whether an item is selected or not.
Originally Posted by humptydumpty
and Plz Check out Once Again for NM_CLICK & item itemchanging Message handler in MSDN
Why - anything we have missed?
-
February 10th, 2006, 07:44 AM
#23
Re: CListCtrl: How to make sure that 1 item is always selected?
Originally Posted by gstercken
Well... Have you missed my post about using NM_CLICK?
Huhh, sorry, to much trouble in this thread...
I tried it and it works as well. So thanks to all for your help!
-
November 3rd, 2008, 01:16 PM
#24
Re: CListCtrl: How to make sure that 1 item is always selected?
It's very simple to deselect all items via keyboard. Just use Ctrl key and space
-
November 3rd, 2008, 01:21 PM
#25
Re: CListCtrl: How to make sure that 1 item is always selected?
Originally Posted by Shchvova
It's very simple to deselect all items via keyboard. Just use Ctrl key and space
Better late than never I guess.
-
November 4th, 2008, 12:20 PM
#26
Sweet sollution!
Sweet and perfectly working sollution
Code:
#define WM_MY_MESSAGE (WM_APP + 1)
BEGIN_MESSAGE_MAP(CGraphRecSett_TemplatesPage,
ON_NOTIFY(LVN_ITEMCHANGED, IDC_LIST, OnLvnItemchangedList)
ON_MESSAGE(WM_MY_MESSAGE, OnMyMessage)
END_MESSAGE_MAP()
void CMyWnd::OnLvnItemchangedListPredefined(NMHDR *pNMHDR, LRESULT *pResult)
{
LPNMLISTVIEW pNMLV = reinterpret_cast<LPNMLISTVIEW>(pNMHDR);
if((pNMLV->uChanged&LVIF_STATE) && (pNMLV->uNewState&LVIS_SELECTED))
{
m_nLastSelectedPredef = pNMLV->iItem; //global int
}
if(!m_nIngibitNextCall && (pNMLV->uChanged&LVIF_STATE) && !(pNMLV->uNewState&LVIS_SELECTED))
{
PostMessage(WM_MY_MESSAGE,0,0);
m_bIngibitNextCall = true;
}
*pResult = 0;
}
LRESULT CMyWnd::OnMyMessage(UINT wParam, LONG lParam)
{
m_bIngibitNextCall= false; //global boolean, dafault false
if(m_ctrlListPredefined.GetSelectedCount()==0)
{
m_ctrlList.SetItemState(m_nLastSelectedPredef, LVIS_SELECTED,LVIS_SELECTED);
}
return 0;
}
m_bIngibitNextCall - just for fun
-
March 19th, 2021, 06:43 AM
#27
Re: CListCtrl: How to make sure that 1 item is always selected?
Thank you guys for the answers, I also found it to be quite a hustle
There is a reason for everything, so after a lot of search that the pointer to the item gets called
1. When the previous item is losing Focus
2. When the previous (OldItem) is being unselected
3. At the time the new selection is being written
The respective flags are:
1. pList->uOldState & LVIS_FOCUSED
2. pList->uOldState & LVIS_SELECTED
3. pList->uNewState & LVIS_SELECTED
It appears that when a user clicks in an empty space, just the second call is retrieved. When changing the selected item by clicking on another item, the three are called in a succession.
The trick here is that GetNextItem(-1, LVNI_FOCUSED) will return the same value as the current item if no item is "changing" selection
Code:
if(p->uOldState & LVIS_SELECTED) // The OldItem is being unselected
{
if ( oListCtrl.GetNextItem(-1, LVNI_FOCUSED) == pListView->iItem)
{
oListCtrl.SetItemState(pListView->iItem, LVIS_SELECTED | LVIS_FOCUSED, LVIS_SELECTED | LVIS_FOCUSED);
}
}
Hope it helps, the other Solutions seem good as well.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|