Click here to Skip to main content
15,894,410 members
Articles / Desktop Programming / MFC

Dynamically Highlight Elements in a List Control

Rate me:
Please Sign up or sign in to vote.
4.91/5 (15 votes)
20 Aug 2001CPOL1 min read 163.7K   2.6K   48   14
Use the custom draw features of a List Control to dynamically find and hightlight items.

Just type a name it gets highlighted!

Introduction

It seems more and more like my projects revolve around the List Control in some form or another. I also seem to be using CodeProject more and more to find out how to do things in my code.

One day, I came across Michael Dunn's excellent article Neat Stuff to do in List Controls Using Custom Draw and it got me to thinking about how to do some really functional stuff with a list control.

I had recently done a project where certain attributes were displayed in a list control. And, at the time, I thought it would be neat if I could color text in a column based on certain criteria, specifically, the text in the cell. Well, due to time constraints, that feature never made it into the project.

But, since then, I've had time to revisit, and have come up with that functionality in this little application.

Simply type in a name in the edit box, and if the name is found in the List Control, it's highlighted yellow and it's text is changed to red.

The specifics of Custom Draw in a List Control are presented in Michael's article, and I've tried to document the code nicely.

Enjoy!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionHow can make the subitem of the listcontrol to be highlighted when the user clicks on a particular subitem Pin
RajaPratap16-Jun-14 0:02
RajaPratap16-Jun-14 0:02 
QuestionPartially Changing background color Pin
Member 783682910-Feb-14 19:54
Member 783682910-Feb-14 19:54 
Questionnice code Pin
sally899812-Dec-11 15:48
sally899812-Dec-11 15:48 
GeneralThanks! Pin
lucy15-Feb-11 9:38
lucy15-Feb-11 9:38 
GeneralGreat article! Pin
monimicki21-Oct-04 17:14
monimicki21-Oct-04 17:14 
Smile | :) This download was just what I was looking for. I wanted to colour individual rows according to whether the text in the first column was the same as that in the previous column - and I coloured the whole line, not just individual items... Only problem is, when you scroll the list view, it doesnt properly paint the items that you have to scroll to to see... So, I'd like to find out how to fix that!!
But anyway, here's how I did the individual coloring:

void CViewOrders::OnCustomList ( NMHDR* pNMHDR, LRESULT* pResult )
{

// This function is based on information I found at The Code Project website.
// I used two articles, one by Michael Dunn found at:
// http://www.thecodeproject.com/listctrl/lvcustomdraw.asp
// And the second by Coleman Brumley found at:
// http://www.thecodeproject.com/listctrl/highlightlistctrl.asp


NMLVCUSTOMDRAW* pLVCD = reinterpret_cast<nmlvcustomdraw*>( pNMHDR );

// Take the default processing unless we set this to something else below.
*pResult = CDRF_DODEFAULT;

// First thing - check the draw stage. If it's the control's prepaint
// stage, then tell Windows we want messages for every item.

if ( CDDS_PREPAINT == pLVCD->nmcd.dwDrawStage )
{
*pResult = CDRF_NOTIFYITEMDRAW;
}
else if ( CDDS_ITEMPREPAINT == pLVCD->nmcd.dwDrawStage )
{
COLORREF crText;

int nItem = static_cast<int>( pLVCD->nmcd.dwItemSpec );

CString Laststr=" ";
CString strTemp = m_OList.GetItemText(nItem,0);
if (nItem>0)
Laststr = m_OList.GetItemText((nItem-1),0);

if(strTemp == Laststr)
{
crText=ChangeCol(1);
}
else
{
crText=ChangeCol(0);
}

// Store the color back in the NMLVCUSTOMDRAW struct.
pLVCD->clrTextBk=crText;

// Tell Windows to paint the control itself.
*pResult = CDRF_DODEFAULT;
}
}



COLORREF CViewOrders::ChangeCol(int g)
{
// Colors were initially set in the initdialog function
//1 means dont change color
// 0 means change
tempcol=lastcol;
lastcol=nextcol;
nextcol=tempcol;

if (g==1)
{
tempcol=lastcol;
lastcol=nextcol;
nextcol=tempcol;
return nextcol;
}
else
{
return nextcol;
}
}
GeneralGreat But Pin
Juha Kiviranta26-Jun-03 21:36
Juha Kiviranta26-Jun-03 21:36 
GeneralRe: Great But Pin
C_BRUMLEY15-Mar-06 17:12
C_BRUMLEY15-Mar-06 17:12 
Generalfiltering Pin
bcengiz9-Oct-02 3:49
bcengiz9-Oct-02 3:49 
GeneralVery usefull Pin
Anonymous30-Jul-02 5:15
Anonymous30-Jul-02 5:15 
GeneralRe: Download Pin
Brian V Shifrin20-Aug-01 12:08
Brian V Shifrin20-Aug-01 12:08 
GeneralRe: Download Pin
20-Aug-01 17:38
suss20-Aug-01 17:38 
GeneralRe: Download Pin
20-Aug-01 18:13
suss20-Aug-01 18:13 
GeneralDownload Pin
FilthyZombie20-Aug-01 3:33
FilthyZombie20-Aug-01 3:33 
GeneralRe: Download Pin
Member 214361124-Jul-05 21:51
Member 214361124-Jul-05 21:51 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.