CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 2 FirstFirst 12
Results 16 to 27 of 27
  1. #16
    Join Date
    Sep 2002
    Location
    14° 39'19.65"N / 121° 1'44.34"E
    Posts
    9,815

    Re: CListCtrl: How to make sure that 1 item is always selected?

    Quote 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.

  2. #17
    Join Date
    Sep 2002
    Location
    14° 39'19.65"N / 121° 1'44.34"E
    Posts
    9,815

    Re: CListCtrl: How to make sure that 1 item is always selected?

    Quote 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.

  3. #18
    Join Date
    Aug 1999
    Location
    Germany
    Posts
    2,338

    Re: CListCtrl: How to make sure that 1 item is always selected?

    Quote 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.

  4. #19
    Join Date
    Aug 1999
    Location
    Germany
    Posts
    2,338

    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.

  5. #20
    Join Date
    Sep 2002
    Location
    14° 39'19.65"N / 121° 1'44.34"E
    Posts
    9,815

    Re: CListCtrl: How to make sure that 1 item is always selected?

    Quote 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.

  6. #21
    Join Date
    May 2005
    Location
    Oregon
    Posts
    3,725

    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

  7. #22
    Join Date
    Sep 2002
    Location
    14° 39'19.65"N / 121° 1'44.34"E
    Posts
    9,815

    Re: CListCtrl: How to make sure that 1 item is always selected?

    Quote 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.

    Quote 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.

    Quote 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.

    Quote Originally Posted by humptydumpty
    and Plz Check out Once Again for NM_CLICK & item itemchanging Message handler in MSDN
    Why - anything we have missed?

  8. #23
    Join Date
    Aug 1999
    Location
    Germany
    Posts
    2,338

    Re: CListCtrl: How to make sure that 1 item is always selected?

    Quote 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!

  9. #24
    Join Date
    Mar 2006
    Posts
    2

    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

  10. #25
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: CListCtrl: How to make sure that 1 item is always selected?

    Quote 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.

  11. #26
    Join Date
    Mar 2006
    Posts
    2

    Talking 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

  12. #27
    Join Date
    Mar 2021
    Posts
    1

    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.

Page 2 of 2 FirstFirst 12

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured