CListCtrl
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12

Thread: CListCtrl

Hybrid View

  1. #1
    Join Date
    Aug 2003
    Posts
    20

    CListCtrl

    Dear all

    I create a dialog and add a List control which is a icon list control.

    i can selete several items if i press Ctrl or Shift.

    now, i want several items being seleted and set focus when a user open this dialog.
    this means if Item 1,2,3 is seleted, then user close dialog. next time when user open the dialog, the Item1,2,3 is seleted and set focus.

    **I just want to know how to set several items' focus, when a user open dialog.

  2. #2
    Join Date
    Sep 2002
    Posts
    924
    You should be able to use CListCtrl::SetItemState to set mutiple items as selected (you would have to call it once for each item you want to select). As for focus, only one item can have the focus, but you can use CListCtrl::SetItemState to do that as well.

  3. #3
    Join Date
    Aug 2003
    Posts
    20
    Hi there

    I used SetItemState, but only the last item will be seleted and get focus;

    Here is my code

    for(int i=0;i<=10;i++)
    {
    m_ImageList.SetItemState(i,LVIS_SELECTED,LVIS_SELECTED);
    }

    Thanks

  4. #4
    Join Date
    Sep 2002
    Posts
    924
    Make sure your listctrl is set to "Always Show Selection", and it should work.

  5. #5
    Join Date
    Sep 2002
    Posts
    924
    Here is quick example I tried:
    Dialog app with CListCtrl (with Always Show Selection = TRUE, and View set to list). CListCtrl member variable added (m_List)
    In initdialog under the todo comments I added following:
    Code:
    	// TODO: Add extra initialization here
    	m_List.SetFocus();
    	CString sLabel;
    	// add 10 items to list
    	for(int i = 0;i < 9; i++)
    	{
    		sLabel.Format("Item%d", i);
    		m_List.InsertItem(i, sLabel);
    	}
    	// select 1st 5 items
    	for(int i = 0;i < 5; i++)
    		m_List.SetItemState(i, LVIS_SELECTED, LVIS_SELECTED );
    	// set focus to first item
    	m_List.SetItemState(0, LVIS_FOCUSED , LVIS_FOCUSED  );
    	
    	// return false because I set focus to a control
    	return false;  // return TRUE  unless you set the focus to a control
    When running the program the 1st 5 items are selected and the 1st item has focus, so it should work for you.
    Last edited by RussG1; February 24th, 2004 at 12:49 AM.

  6. #6
    Join Date
    Aug 2003
    Posts
    20
    Thank you for your help, i am going to try it now.

  7. #7
    Join Date
    Sep 2002
    Location
    14 39'19.65"N / 121 1'44.34"E
    Posts
    9,815
    Originally posted by wow9999
    I used SetItemState, but only the last item will be seleted and get focus;
    You should also make sure that your list control doesn't have the LVS_SINGLESEL style set.

  8. #8
    Join Date
    May 1999
    Location
    Southern California
    Posts
    12,266
    The third Parameter of SetItemState is a mask value and LVIS_SELECTED is not a mask value.
    "Signature":
    My web site is Simple Samples.
    C# Corner Editor

  9. #9
    Join Date
    Sep 2002
    Posts
    924
    Originally posted by Sam Hobbs
    The third Parameter of SetItemState is a mask value and LVIS_SELECTED is not a mask value.
    The mask parameter for SetItemState is a stateMask and LVIS_SELECTED can be used for this parameter to tell SetItemState that you want only the items selection bits to be changed, so you can use LVIS_SELECTED as a mask value in this case.

    Russ

  10. #10
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,545
    This actually is documented.
    From MSDN:
    nState
    New values for the state bits.
    nMask
    Mask specifying which state bits to change.
    You need a mask to be able to "drop" a particular state, like in:
    Code:
    m_List.SetItemState(item, 0, LVIS_SELECTED);
    Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
    Convenience and productivity tools for Microsoft Visual Studio:
    FeinViewer - an integrated GDI objects viewer for Visual C++ Debugger, and more...

  11. #11
    Join Date
    Sep 2002
    Posts
    924
    Yes, I agree the docs are a bit lacking here, but it does say the mask value for SetItemState specifies which state bits to change. LVIS_SELECTED is a state bit, whereas LVIF_IMAGE is not. If you dig a little deeper and look at the version that takes a LVITEM structure as a parameter, and lookup the LVITEM structure itself, you will find one of the parameters is stateMask, and the decription of that is as follow:

    Value specifying which bits of the state member will be retrieved or modified. For example, setting this member to LVIS_SELECTED will cause only the item's selection state to be retrieved.
    This member allows you to modify one or more item states without having to retrieve all of the item states first. For example, setting this member to LVIS_SELECTED and state to zero will cause the item's selection state to be cleared, but none of the other states will be affected.


    From this I am making a small leap in thinking that the same thing goes for the mask value of SetItemState since it says the mask value only specifies which state bits to change (as does the decription of stateMask), and that the docs should probably say stateMask rather than mask for that parameter, to be more clear (a list of valid mask value's would be nice too).

    If you wanna to be completely safe then just do it using an LVITEM structure instead, but personally I think that this is what is meant for the mask value for SetItemState, otherwise you really have nothing to use for the mask value in SetItemState, and the function is almost useless.

    Russ

  12. #12
    Join Date
    May 1999
    Location
    Southern California
    Posts
    12,266
    Originally posted by RussG1
    The mask parameter for SetItemState is a stateMask and LVIS_SELECTED can be used for this parameter to tell SetItemState that you want only the items selection bits to be changed, so you can use LVIS_SELECTED as a mask value in this case.

    Russ
    Yes.

    I am sorry that I was confused.
    "Signature":
    My web site is Simple Samples.
    C# Corner Editor

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center