Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Programatically Highlighting A Row In A CListCtrl

2,693 views
Skip to first unread message

William Wonneberger

unread,
Oct 19, 1999, 3:00:00 AM10/19/99
to
Greetings;

Can anyone share an idea or some sample code on how I can highlight a row
inside a CListCtrl, as though the user had clicked the mouse pointer on the
specified row?

Any help would be greatly appreciated, and I'll gladly post any findings.

Thank you in advance for your time and help...
--
Bill Wonneberger
wbe...@locke.ccil.org
www.ccil.org/~wberger/

Yong Zhao

unread,
Oct 19, 1999, 3:00:00 AM10/19/99
to

William Wonneberger wrote in message ...

>Greetings;
>
>Can anyone share an idea or some sample code on how I can highlight a row
>inside a CListCtrl, as though the user had clicked the mouse pointer on the
>specified row?
>

using yourListCtrl.SetItemState(nItem, LVIS_SELECTED, LVIS_SELECTED);
yourListCtrl.Update(nItem);

Yong

William Wonneberger

unread,
Oct 19, 1999, 3:00:00 AM10/19/99
to

Yong Zhao <yz...@axiomsys.com> wrote in message
news:#8Q7c1nG$GA.250@cppssbbsa04...

...

>
> using yourListCtrl.SetItemState(nItem, LVIS_SELECTED, LVIS_SELECTED);
> yourListCtrl.Update(nItem);
>

...

Thanks for the suggestion Yong, but I couldn't get this to work.
CListCtrl::SetItemState() does change what the CListCtrl keeps as the index
of the selected list item, but it doesn't highlight the selected item.
Since Update is in the CWnd base class, I also tried using UpdateWindow(),
but like your previous suggestion, I couldn't get it to work either.

There is one thing I forgot to mention in my original posting. The
CListCtrl instance in my application is my own CListCtrl derived object,
which highlights the entire row of the list control when it's in report
mode. This method is described in the Microsoft KB article Q230049. In my
derived class, I've overridden the CListCtrl implementation of
CListCtrl::OnCreate() using the following;

int CRowSelListCtrl::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
TRACE("\n\n Entering CRowSelListCtrl::OnCreate()\n");

if (CListCtrl::OnCreate(lpCreateStruct) == -1)
return -1;

DWORD dwStyle = ::SendMessage(m_hWnd, LVM_GETEXTENDEDLISTVIEWSTYLE,
0, 0);
dwStyle |= LVS_EX_FULLROWSELECT;

::SendMessage(m_hWnd, LVM_SETEXTENDEDLISTVIEWSTYLE, 0, dwStyle);

TRACE("\n Leaving CRowSelListCtrl::OnCreate()\n\n");

return 0;
}

Would this keep either Update() or UpdateWindow() from highlighting the
specified row? Or, am I missing something else?

Thanks again for your time and help. Any additional ideas or insights would
be greatly appreciated.

SnoopSoft

unread,
Oct 20, 1999, 3:00:00 AM10/20/99
to
I've used the following code with good success:

myListCtrl.SetItemState(nNewSelection, LVIS_SELECTED | LVIS_FOCUSED,
0x000F);

--
----
Chris Scott
SnoopSoft Corporation
http://www.snoopsoft.com


William Wonneberger <wbe...@locke.ccil.org> wrote in message
news:ut$aptnG$GA.251@cppssbbsa05...


> Greetings;
>
> Can anyone share an idea or some sample code on how I can highlight a row
> inside a CListCtrl, as though the user had clicked the mouse pointer on
the
> specified row?
>

Vaughn Arthur

unread,
Oct 20, 1999, 3:00:00 AM10/20/99
to
// select it
m_testlist.SetItemState(m_currentline,LVIS_SELECTED | LVIS_FOCUSED ,
LVIS_SELECTED | LVIS_FOCUSED);
// scroll it to make sure it's visible
m_testlist.EnsureVisible(m_currentline,FALSE);
// Show It
m_testlist.SetExtendedStyle(LVS_EX_FULLROWSELECT | LVS_EX_SUBITEMIMAGES |
LVS_EX_GRIDLINES);

Yong Zhao

unread,
Oct 20, 1999, 3:00:00 AM10/20/99
to
Will,

Try youListCtrl.SetFocus()
Or if you don't want to set your control to focus, you may need to set the
following style when your create your list control:
LVS_SHOWSELALWAYS

Yong.

William Wonneberger

unread,
Oct 21, 1999, 3:00:00 AM10/21/99
to
Greetings;

First and foremost, my sincerest thanks to everyone who helped.

I did get the following to work;

m_iListIndex = m_lstMyList.FindItem(&lvfSearch, m_iListIndex);

if(m_iListIndex != -1)
{
m_lstMyList.SetItemState(m_iListIndex, LVIS_SELECTED |
LVIS_FOCUSED, 0x000F);
m_lstMyList.EnsureVisible(m_iListIndex, FALSE);
}

And to "deselect" a selected item....

if(m_iListIndex != -1)
{
uState = m_lstMyList.GetItemState(m_iListIndex, LVIS_SELECTED);

if(uState)
m_lstMyList.SetItemState(m_iListIndex, 0, LVIS_SELECTED);
}

The call of CListCtrl::SetItemState() as shown in the first highlighting
code fragement was courtesy of Chris Scott at SnoopSoft Corporation. I am
also glad that a similar solution I received from Vaughn Arthur included
CListCtrl::EnsureVisible(), which I originally overlooked in going through
the MFC CListCtrl class member documentation. CListCtrl::EnsureVisible()
will scroll the list control focus to the specified item if it isn't already
visible. I also received a number of suggestions from Yong Zhao which also
helped shed some light on my coding problem.

Again, many thanks to Chris Scott, Vaughn Arthur, and Yong Zhao for their
time and help...

Best Regards...

Message has been deleted
Message has been deleted

Robert Fritz

unread,
Aug 16, 2017, 7:29:13 AM8/16/17
to
Thanks. I was searching for the de-select code.

My implementation uses the extended style LVS_EX_CHECKBOXES which puts a checkbox next to the text label in the first column.

The checkbox alters the normal selection behavior in that toggling the check-box on any row does not automatically select & focus that row. And I wanted to emulate that behavior, including GetKeyState(VK_SHIFT) and GetKeyState(VK_CONTROL).

Incidentally, the x000F used above is the logical OR of:
LVIS_SELECTED | LVIS_FOCUSED | LVIS_CUT | LVIS_DROPHILITED

So if your implementation doesn't require LVIS_CUT, defined as "The item is marked for a cut-and-paste operation", you can omit that mask.

0 new messages