CListCtrl - Problems with selection state 
Author Message
 CListCtrl - Problems with selection state

Hi,

Another newbie question about CListCtrls.

I have written some code, and can do most things.  So far, so good.
However, after a person clicks on an item to set the state, the
selection state remains set even if the item isn't highlighted.  I
have written some code so that a double click on the item will
initiate some action.  The problem is that the double click is
processed on the control, and not the record.  So from then on
out, the user can double click on the control and the previously
processed record is once again processed, even though it is no
longer highlighted.  I tried to "de-set" the selection state with
the following call, but it didn't appear to work.  (Or probably
more to the point, I don't truly understand what is happening,
and don't find much about this in the books...)

        m_cList.SetItemState( iItem, 0, 0 );

What I'm ultimately after is a way to set the state back to
unselected after processing a record, so that a call to

        iItem = m_cList.GetSelectionMark();

will return a -1 until another record is double clicked or
highlighted.  Also a way to programmatically highlight a
record would be good, too.  I tried that elsewhere and it
also didn't work.  I used SetSelectMark( Item, 1, 1 ) for
that call.

Thanks in advance,
Connie Nusser



Mon, 27 Aug 2001 03:00:00 GMT  
 CListCtrl - Problems with selection state

Quote:

> highlighted.  Also a way to programmatically highlight a
> record would be good, too.  I tried that elsewhere and it
> also didn't work.  I used SetSelectMark( Item, 1, 1 ) for
> that call.

Oops, typos.  I really used:  m_cList.SetSelectionMark(iItem);

Connie Nusser



Mon, 27 Aug 2001 03:00:00 GMT  
 CListCtrl - Problems with selection state
Connie,

On Thu, 11 Mar 1999 17:53:01 +0000, Connie Nusser

Quote:

>    m_cList.SetItemState( iItem, 0, 0 );

>What I'm ultimately after is a way to set the state back to
>unselected after processing a record, so that a call to

>    iItem = m_cList.GetSelectionMark();

>will return a -1 until another record is double clicked or
>highlighted.  Also a way to programmatically highlight a
>record would be good, too.  I tried that elsewhere and it
>also didn't work.  I used SetSelectMark( Item, 1, 1 ) for
>that call.

You were close.  To "unselect":

        m_cList.SetItemState( iItem, 0, LVIS_SELECTED );

To select:

        m_cList.SetItemState( iItem, LVIS_SELECTED, LVIS_SELECTED );

If you check the docs on CListCtrl::SetItemState(), you'll see that
you have to set the nMask to tell it to which bit values in nState it
should pay attention.

Chip

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
"Make it idiot-proof and someone will make a better idiot."
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

Remove NO and SPAM from my email address.



Tue, 28 Aug 2001 03:00:00 GMT  
 CListCtrl - Problems with selection state

Quote:

> You were close.  To "unselect":

>         m_cList.SetItemState( iItem, 0, LVIS_SELECTED );

> To select:

>         m_cList.SetItemState( iItem, LVIS_SELECTED, LVIS_SELECTED );

> If you check the docs on CListCtrl::SetItemState(), you'll see that
> you have to set the nMask to tell it to which bit values in nState it
> should pay attention.

Chip, thanks for your answer.  Unfortunately, neither one of these
lines did what I want, so perhaps I am not describing it correctly.

I have a ListCtrl that the user can double click to get more
info about that item on a modal dialog that pops up after the
double click.  All well and good, that works fine.  Then, after
they return to the first dialog, the item they previous double
clicked is no longer highlighted, so I assumed that it was no
longer selected.  But to my surprise, they person can double
click on the list control anywhere (not just the first column)
and the item they just looked at once again is displayed to them.
EVEN THOUGH IT WAS NO LONGER VISIBLY SELECTED (highlighted)!!  
I think this would be very confusing to the user, so I want to
programmatically de-select it, although I personally think it's
a bug that a list item that is not highlighted would still be
considered selected.  So I just used the line of code from
above to de-select the item, but the item will still return
as selected and display if the list is double clicked.

Also, I have another list that I want to select the first
item if it is the only item in the list.  Using the line of
code above, it still does not highlight (or select) using
the call above.  In this dialog, the user can either double
click an item, or single click it to select an item, then
hit OK. I wanted it to default to the first item selected
if it was the only item in the list, so the code is at the
bottom of the InitDialog code after checking the list count.  
But even though I put code in as above, when the OnOK logic
runs through, it does not find the selected item.  Here,
there has to be an item manually selected before the OnOK
will pick up the item as selected.  

Please, please help me to understand the default behavior
of the list control.  To me, if the item is not highlighted,
it should never return as selected.  The call I'm using to
get the selected item is:

   long iItem = m_cList.GetSelectionMark();

Please note that this will return an item even if no records
in the list are highlighted.  (And even though I have already
made the following call to de-select it after the previous
selection:  m_cList.SetItemState( iItem, 0, LVIS_SELECTED ); )

Thanks for any help on this matter!

Connie Nusser



Wed, 29 Aug 2001 03:00:00 GMT  
 CListCtrl - Problems with selection state

Quote:

> Connie,

> On Thu, 11 Mar 1999 17:53:01 +0000, Connie Nusser
> You were close.  To "unselect":

>         m_cList.SetItemState( iItem, 0, LVIS_SELECTED );

> To select:

>         m_cList.SetItemState( iItem, LVIS_SELECTED, LVIS_SELECTED );

> If you check the docs on CListCtrl::SetItemState(), you'll see that
> you have to set the nMask to tell it to which bit values in nState it
> should pay attention.

Hi, Chip,

I played around with this some more, and no have the "unselect"
working.  Or at least the looping the way I wanted it to.  The
unselect probably always worked, but I was finding the index
using GetSelectionMark().  When I changed that to GetNextItem(),
then it started working the way I wanted it to.

However, I still cannot get the SetItemState(), coded as above,
to programmatically select an item.  I really want to highlight
the item for the user when there is only one item to reduce the
key strokes for the user.

Also, since I see you answer many of the ListCtrl questions on
this NG, perhaps you could tell me how to extend the control to
select the full row and not just the first column.  I have looked
through the rowlist sample, and it is so complex, way more than I
need as an MFC beginner, and I don't really get it from that
sample.  My list control is created in the dialog editor, and
there is no option of an extended style for a full row select,
although I see you can set it via a call. I just don't find the
exact call to do so, how and where.

It all boils down to I guess i don't really understand all the
finer points of the control.  The GetSelectionMark() call gave
the correct index of the selected control, but it continued to
do so even after I had changed the item state to unselected, where
GetNextItem honors the state change.

Thanks for all your help!

Connie Nusser



Wed, 29 Aug 2001 03:00:00 GMT  
 CListCtrl - Problems with selection state
Connie,

Quote:
>However, I still cannot get the SetItemState(), coded as above,
>to programmatically select an item.  I really want to highlight
>the item for the user when there is only one item to reduce the
>key strokes for the user.

This probably has to do with the fact that you  are selecting the
first item.  You should also set the focus to that item like this:

UINT uState = LVIS_SELECTED | LVIS_FOCUSED;
UINT uMask = LVIS_SELECTED | LVIS_FOCUSED;
m_cList.SetItemState(0, uState, uMask);

Make sure that the focus is also set to the the control:

CWnd *pOldWnd = m_cList.SetFocus();

Quote:
>...perhaps you could tell me how to extend the control to
>select the full row and not just the first column.

There may be a macro defined to do this, but here's the basic way:

m_cList.SendMessage(LVM_SETEXTENDEDLISTVIEWSTYLE,
LVS_EX_FULLROWSELECT, LVS_EX_FULLROWSELECT);

You should also check the following article on www.codeguru.com.  It
covers many of the extended styles that aren't covered by the docs (at
least in VC++ 5.0).

http://www.codeguru.com/listview/ie4_extended_styles_to_a_list_contro...

Chip

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
"Make it idiot-proof and someone will make a better idiot."
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

Remove NO and SPAM from my email address.



Sat, 01 Sep 2001 03:00:00 GMT  
 CListCtrl - Problems with selection state
Thank you Chip!  You have been very helpful.  I now have the full
select working, and will next try the set selection.  Sounds very
logical what wrote, as I was not setting any focus.

Thanks!  
Connie Nusser

Quote:

> Connie,

> >However, I still cannot get the SetItemState(), coded as above,
> >to programmatically select an item.  I really want to highlight
> >the item for the user when there is only one item to reduce the
> >key strokes for the user.

> This probably has to do with the fact that you  are selecting the
> first item.  You should also set the focus to that item like this:

> UINT uState = LVIS_SELECTED | LVIS_FOCUSED;
> UINT uMask = LVIS_SELECTED | LVIS_FOCUSED;
> m_cList.SetItemState(0, uState, uMask);

> Make sure that the focus is also set to the the control:

> CWnd *pOldWnd = m_cList.SetFocus();

> >...perhaps you could tell me how to extend the control to
> >select the full row and not just the first column.

> There may be a macro defined to do this, but here's the basic way:

> m_cList.SendMessage(LVM_SETEXTENDEDLISTVIEWSTYLE,
> LVS_EX_FULLROWSELECT, LVS_EX_FULLROWSELECT);

> You should also check the following article on www.codeguru.com.  It
> covers many of the extended styles that aren't covered by the docs (at
> least in VC++ 5.0).

> http://www.codeguru.com/listview/ie4_extended_styles_to_a_list_contro...

> Chip

> -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
> "Make it idiot-proof and someone will make a better idiot."
> -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

> Remove NO and SPAM from my email address.



Mon, 03 Sep 2001 03:00:00 GMT  
 CListCtrl - Problems with selection state
Chip,

Thanks again for all the help. Put the focus code in today, and
everything is working wonderfully!  It so hard to change operating
systems and languages (C to C++) and all the help you and others
have provided on this NG has been invaluable.

Connie Nusser

Quote:

> Connie,

> >However, I still cannot get the SetItemState(), coded as above,
> >to programmatically select an item.  I really want to highlight
> >the item for the user when there is only one item to reduce the
> >key strokes for the user.

> This probably has to do with the fact that you  are selecting the
> first item.  You should also set the focus to that item like this:

> UINT uState = LVIS_SELECTED | LVIS_FOCUSED;
> UINT uMask = LVIS_SELECTED | LVIS_FOCUSED;
> m_cList.SetItemState(0, uState, uMask);

> Make sure that the focus is also set to the the control:

> CWnd *pOldWnd = m_cList.SetFocus();

> >...perhaps you could tell me how to extend the control to
> >select the full row and not just the first column.

> There may be a macro defined to do this, but here's the basic way:

> m_cList.SendMessage(LVM_SETEXTENDEDLISTVIEWSTYLE,
> LVS_EX_FULLROWSELECT, LVS_EX_FULLROWSELECT);

> You should also check the following article on www.codeguru.com.  It
> covers many of the extended styles that aren't covered by the docs (at
> least in VC++ 5.0).

> http://www.codeguru.com/listview/ie4_extended_styles_to_a_list_contro...

> Chip

> -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
> "Make it idiot-proof and someone will make a better idiot."
> -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

> Remove NO and SPAM from my email address.



Tue, 04 Sep 2001 03:00:00 GMT  
 
 [ 8 post ] 

 Relevant Pages 

1. CToolBar selection state drawing problem!!

2. CListCtrl selection problem

3. CListCtrl Multi Selection Problem

4. CListCtrl selection problem....

5. CListCtrl item selection problem

6. CListCtrl Owner data and selection problem

7. CListView - click on state icon and selection

8. Check Selected State in CListCtrl of MFC

9. Checkbox state in CListCtrl

10. HELP: State sensitive icon display in CListCtrl

11. CListCtrl setting state

12. CListCtrl Extended Selection w/ a mouse

 

 
Powered by phpBB® Forum Software