CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 27
  1. #1
    Join Date
    Aug 1999
    Location
    Germany
    Posts
    2,338

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

    Hello!

    I have a CListCtrl with a single-selection in report-view. I want to make sure that always one of the items is selected.

    The problem is:

    I capture the ItemChanged-Notification. When selecting a new item, its called twice:
    1.) To show that the old item was deselected
    2.) To show that the new item was selected

    When clicking in an area where no items are, you just get:
    1.) To show that the old item was deselected

    That means in this step there is no way to distinguish if another item will be selected or not.

    Any ideas?

  2. #2
    Ejaz's Avatar
    Ejaz is offline Elite Member Power Poster
    Join Date
    Jul 2002
    Location
    Lahore, Pakistan
    Posts
    4,211

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

    Try "Show selection always" property of CListCtrl checked and see if it serves the purpose for you.

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

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

    Hi Ejaz,

    thanks for you reply. Unfortunatly this is not what I was looking for. Altough the "show selction always" state is set, it is possible that no selection occurs.

  4. #4
    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
    thanks for you reply. Unfortunatly this is not what I was looking for. Altough the "show selction always" state is set, it is possible that no selection occurs.
    So if I understand you correctly, you just want to check whether a new item has been selected or not? One way to do that to examinine the uNewState and uOldState members is the NMLISTVIEW structure you get along with the notification message.

  5. #5
    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
    So if I understand you correctly, you just want to check whether a new item has been selected or not?
    Yes.

    Quote Originally Posted by gstercken
    One way to do that to examinine the uNewState and uOldState members is the NMLISTVIEW structure you get along with the notification message.
    No Because this is exactly what I tried. If I select a new item, I get 2 notification-messages:
    1.) To show that the old item was deselected (uOldState: selected, uNewState: not selected)
    2.) To show that the new item was selected (uOldState: not selected, uNewState: selected)

    If I click somewhere in the ListCtrl where no item is, I get one message:
    1.) To show that the old item was deselected (uOldState: selected, uNewState: not selected)

    But no 2nd message is called. So I have to do something in 1.) without knowing if 2.) will happen somewhere in the near future.


    Example:
    If uOldState == selected && uNewState != selected:
    pList->SetItemState(nItem, LVIS_SELECTED, LVIS_SELECTED); // If the item is deselected, select it again.

    The effect of such an approach doing something in 1.) is either that I cannot change the selection at all or that the selection is flickering because the old item is selected for a short period of time again when switching to a new item.
    Last edited by martho; February 10th, 2006 at 06:06 AM.

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

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

    Quote Originally Posted by martho
    Hello!

    I have a CListCtrl with a single-selection in report-view. I want to make sure that always one of the items is selected.

    The problem is:

    I capture the ItemChanged-Notification. When selecting a new item, its called twice:
    1.) To show that the old item was deselected
    2.) To show that the new item was selected

    When clicking in an area where no items are, you just get:
    1.) To show that the old item was deselected

    That means in this step there is no way to distinguish if another item will be selected or not.

    Any ideas?
    1) From List Control Property Select Single Selection for selcting
    one Item at a time .In this case you can't select more then one item at a single time

    2)for Capturing The property of Item Change from ListCtrl handle use message handler LVN_ITEMCHANGING

    3)now make a Loop to get Which Item is Selected to know whether that item has some text or empty row only.by following method you can get that your selected row and can check whether selected row has some text or not.
    Code:
    int n=m_ListCtrl2.GetItemCount();
       for(int i=0;i<n+1;i++)
       {
    	if(m_ListCtrl2.GetItemState(i, LVIS_SELECTED) == LVIS_SELECTED)
    	{
    //you will get your selected item here and can retrive their value by GetItemtext  if it is empty show that  your selected item is a empty row and do what ever you want to do in this case either change the fous of listcontrol on some other or or wat ever
    	}
       }
    Thankyou

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

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

    Hi humptydumpty,

    I did set "Select Single Selection" from the start and I have a handler for OnLvnItemChanged.

    But what would the loop give me then? I have no items in the ListCtrl which have no text.

    Imaging I have 3 items of text in the report-view and the ListCtrl is high enought to keep 5 items without scrolling. Item 1 is selected. Now I click in the space between the 3rd item and the bottom of the ListCtrl. The effect is that I get ONE message telling me the item 1 has been deselected. In this state NO ITEM is selected. I get NO MORE additional messages here.

    Imagine now the same ListCtrl, item 1 is select. Now I click on item 2. The effect is that I get one message telling me that item 1 has been deselected - in this state NO ITEM is selected (which I cannot distinguish from the state above!!)

    THEN, some time later, I get another message telling me that item 2 has been selected.

  8. #8
    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 did set "Select Single Selection" from the start and I have a handler for OnLvnItemChanged.

    But what would the loop give me then? I have no items in the ListCtrl which have no text.
    And in addition to all that, your loop runs too far:
    Quote Originally Posted by humptydumpty
    Code:
    int n=m_ListCtrl2.GetItemCount();
       for(int i=0;i<n+1;i++)
       {
       }
    It should be i < n instead of n-1.


    Quote Originally Posted by humptydumpty
    if this post helps your Don't Forget to Rate
    I'm not sure if I understand what a "Don't" is and if your post helped my "Don't" - but in any case, I followed your advice and forgot to rate...
    Last edited by gstercken; February 10th, 2006 at 06:34 AM.

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

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

    See Martho
    1) First Try LVN_ITEMCHANGING and see what's the result you are going to get.in this case if First Item is Selected and you select second item that time only you will get notification that second item is selected.

    2) Regarding for Loop if you click on Empty Row you are not Going to get any text from Selected row that time you can judge that this item doesn't contain any item.and can prompt a message to user empty Item or No Item to be Selected and can set the Focus of selected item on Previous Item only. you have to maintain a counter to check what was the last selected item in your ListCtrl.

    give it one try and let me know if still you are getting your Problem.

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

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

    Quote Originally Posted by gstercken
    And in addition to all that, your loop runs too far:
    It should be i < n instead of n-1.


    I'm not sure if I understand what a "Don't" is and if your post helped my "Don't" - but in any case, I followed your advice and forgot to rate...
    yes abs right i didn't check this code.simply write this code. thanx.
    and it should be like i < n this only.

    I'm not sure if I understand what a "Don't" is and if your post helped my "Don't" - but in any case, I followed your advice and forgot to rate...
    and really that's much good .even i don't except points from you for this type of mistake .but i rated you to catch this mistake.
    Last edited by humptydumpty; February 10th, 2006 at 06:44 AM.

  11. #11
    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
    1) First Try LVN_ITEMCHANGING and see what's the result you are going to get.in this case if First Item is Selected and you select second item that time only you will get notification that second item is selected.
    No, that's not true. You will still get the same number of notifications in the same order. The only difference with LVN_ITEMCHANGING is that you get a chance to prevent the selection change.

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

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

    Quote Originally Posted by gstercken
    No, that's not true. You will still get the same number of notifications in the same order. The only difference with LVN_ITEMCHANGING is that you get a chance to prevent the selection change.
    yes gstercken it's true.just handle this message and make a for loop and you will get which item is selected .and that is only the actual req .and you can put all the necc condition here.

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

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

    I agree with gstrecken, but I coded it anyway:

    Code:
    void CDiaPrint::OnLvnItemchangingListPrinter(NMHDR *pNMHDR, LRESULT *pResult)
    {
    	LPNMLISTVIEW pNMLV = reinterpret_cast<LPNMLISTVIEW>(pNMHDR);
    	if (pNMLV->uChanged & LVIF_STATE &&
    		pNMLV->uNewState & LVIS_SELECTED)
    	{	TRACE("Item selected\n");
    		int nCount = 0;
    		for (int i = 0; i < m_ListPrinters.GetItemCount(); i++)
    		{	if (m_ListPrinters.GetItemState(i, LVIS_SELECTED) == LVIS_SELECTED)
    			{	nCount++;
    			}
    		}
    		TRACE("%i Items are selected\n", nCount);
    	}
    
    	if (pNMLV->uChanged & LVIF_STATE &&
    		!(pNMLV->uNewState & LVIS_SELECTED))
    	{	TRACE("Item deselected\n");
    		int nCount = 0;
    		for (int i = 0; i < m_ListPrinters.GetItemCount(); i++)
    		{	if (m_ListPrinters.GetItemState(i, LVIS_SELECTED) == LVIS_SELECTED)
    			{	nCount++;
    			}
    		}
    		TRACE("%i Items are selected\n", nCount);
    	}
    
    	*pResult = 0;
    }
    Result:
    Clicking on an empty area:
    Item deselected
    1 Items are selected


    Clicking on another item:
    Item deselected
    1 Items are selected

    Item selected
    0 Items are selected


    So still no way to distinguish between the 2 cases.

  14. #14
    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
    just handle this message and make a for loop and you will get which item is selected .and that is only the actual req .and you can put all the necc condition here.
    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.

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

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

    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.

Page 1 of 2 12 LastLast

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