How to use different color to display the item text in a ListView class with View property set to "d

  • Thread starter Thread starter ZhangZQ
  • Start date Start date
Z

ZhangZQ

How to use different color to display the item text in a ListView class with
View property set to "details"?

I want to use different color for different row in the ListView.

Thank you very much!



Regards,
ZhangZQ
 
Hi ZhangZQ,

Do you mean the ListViewItem.BackColor property?

bool swap = false;
foreach(ListViewItem l in lv.Items)
{
if(swap)
{
l.BackColor = SystemColors.ControlText;
l.ForeColor = SystemColors.ControlLightLight;
}
swap = !swap;
}

This will cause every other line to be reverse colored.

Note that selected lines won't be affected.

If you want to set different colors in different columns you need to set UseItemStyleForSubItems = false on the listviewitems.
 
Zhang,

I'm sorry, but I'm not familiar with Compact Framework, so I can't tell you how.
Maybe you can use a DataGrid/DataView and make it look like a ListView.
 
Hi,

First of all you should limit your self to the NG related to this
,microsoft.public.dotnet.framework.clr has nothing to do with this.
You can use the opennetcf implementation.
IIRC the original had not that feature, maybe Alex fierman, or christ tacke
can give you more details,
If you check the discussion forums of opennetcf you will see a couple of
posts I did with the modification for this to happen. I havent checked the
site in a while so I do not for sure if this was included or not.
Here are the changes that you need to do ;


1- Add a ForeColor/BackColor to ListItem
private Color _forecolor;
private Color _backcolor;

public Color ForeColor
{
get{ return _forecolor;}
set{ _forecolor = value;}
}
public Color BackColor
{
get{ return _backcolor;}
set{ _backcolor = value;}
}

Change the OnDrawItem method to draw it with the correct color:

if (e.State == DrawItemState.Selected)
{
//Highlighted
e.DrawBackground();
textBrush = new SolidBrush(SystemColors.HighlightText);
}
else
{
if ( item.BackColor != Color.Empty)
e.DrawBackground( item.BackColor);
else
e.DrawBackground(this.BackColor);

if ( item.ForeColor !=Color.Empty )
textBrush = new SolidBrush( item.ForeColor);
else
textBrush = new SolidBrush( e.ForeColor);
}



I think that it's all what you need.

Just post back if not and CHECK opennetcf

cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation


ZhangZQ said:
Morten Wennevik

Yes, that is what I want, thank you very much, but I am using compact
framework and the BackColor/ForeColor properties can be found in compact
framework, how should I do? By using the owner-draw can do this, but how to
implement owner-draw in compact framework?


Regards,
ZhangZQ



Morten Wennevik said:
Hi ZhangZQ,

Do you mean the ListViewItem.BackColor property?

bool swap = false;
foreach(ListViewItem l in lv.Items)
{
if(swap)
{
l.BackColor = SystemColors.ControlText;
l.ForeColor = SystemColors.ControlLightLight;
}
swap = !swap;
}

This will cause every other line to be reverse colored.

Note that selected lines won't be affected.

If you want to set different colors in different columns you need to set
UseItemStyleForSubItems = false on the listviewitems.
 
Morten Wennevik

Yes, that is what I want, thank you very much, but I am using compact
framework and the BackColor/ForeColor properties can be found in compact
framework, how should I do? By using the owner-draw can do this, but how to
implement owner-draw in compact framework?


Regards,
ZhangZQ



Morten Wennevik said:
Hi ZhangZQ,

Do you mean the ListViewItem.BackColor property?

bool swap = false;
foreach(ListViewItem l in lv.Items)
{
if(swap)
{
l.BackColor = SystemColors.ControlText;
l.ForeColor = SystemColors.ControlLightLight;
}
swap = !swap;
}

This will cause every other line to be reverse colored.

Note that selected lines won't be affected.

If you want to set different colors in different columns you need to set
UseItemStyleForSubItems = false on the listviewitems.
 
Ê×ÏÈÉèÖÃÒ»¸örowµÄcolor,ºóÃæµÄ¾ÍµÈÓÚÇ°ÃæµÄcolor+³£Á¿£¬¾Í¿ÉÒÔÁË¡£
 
Ignacio Machin,

Thank you very much!



Regards,
ZhangZQ


Ignacio Machin ( .NET/ C# MVP ) said:
Hi,

First of all you should limit your self to the NG related to this
,microsoft.public.dotnet.framework.clr has nothing to do with this.
You can use the opennetcf implementation.
IIRC the original had not that feature, maybe Alex fierman, or christ tacke
can give you more details,
If you check the discussion forums of opennetcf you will see a couple of
posts I did with the modification for this to happen. I havent checked the
site in a while so I do not for sure if this was included or not.
Here are the changes that you need to do ;


1- Add a ForeColor/BackColor to ListItem
private Color _forecolor;
private Color _backcolor;

public Color ForeColor
{
get{ return _forecolor;}
set{ _forecolor = value;}
}
public Color BackColor
{
get{ return _backcolor;}
set{ _backcolor = value;}
}

Change the OnDrawItem method to draw it with the correct color:

if (e.State == DrawItemState.Selected)
{
//Highlighted
e.DrawBackground();
textBrush = new SolidBrush(SystemColors.HighlightText);
}
else
{
if ( item.BackColor != Color.Empty)
e.DrawBackground( item.BackColor);
else
e.DrawBackground(this.BackColor);

if ( item.ForeColor !=Color.Empty )
textBrush = new SolidBrush( item.ForeColor);
else
textBrush = new SolidBrush( e.ForeColor);
}



I think that it's all what you need.

Just post back if not and CHECK opennetcf

cheers,
 
Back
Top