List view colour question

P

Patrick De Ridder

Below a rather simplistic example of a list view routine.
Say I want to have "over" appearing in red on the
display, how would I do that?
Many thanks,
Patrick.
(e-mail address removed)
***************************
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
\namespace WindowsApplication1
{
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.ListView listView1;
private System.Windows.Forms.ColumnHeader columnHeader1;
private System.Windows.Forms.ColumnHeader columnHeader2;
private System.ComponentModel.Container components = null;
public Form1()
{
InitializeComponent();
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region
#endregion
static void Main()
{
Application.Run(new Form1());
}
private void Form1_Load(object sender, System.EventArgs e)
{
string [] content = new string [4];
string [] show = new string [2];
content [0] = "the cat";
content[1] = "jumped ";
content [2] = "over";
content[3] = "the wall";
int k = -1;
for (int i=0 ;i<2;i++)
{
show [0] = content[++k];
show[1] = content [++k];
ListViewItem list = new ListViewItem(show);
listView1.Items.Add(list);
}
}
}
}
 
K

Kieran Benton

You can do
list.ForeColor = Color.Red
(I think thats the spelling, sorry only in newsreader)

You can even set sub items to different colours if you set
list.UseSameFormatForAllSubItems (or similar again :) ) to true

HTH
Kieran
 
P

Patrick De Ridder

You can do
list.ForeColor = Color.Red

This would set al cells to red, I don't want that!
Re my question: I want to be able to set one cell to red under certain
conditions (specifically if the value is negative).
(I think thats the spelling, sorry only in newsreader)

You can even set sub items to different colours if you set
list.UseSameFormatForAllSubItems (or similar again :) ) to true

I don't understand this.

Please give further help, I am not cracking this problem.

Thanks.
 
K

Kieran Benton

The correct spelling of the property is:
list.UseItemStyleForSubItems = false

Once this is set you can set individual columns in a row to different
styles.

e.g.

ListViewItem list = listView1.Items.Add( "item" + i.ToString() );
list.UseItemStyleForSubItems = false;
ListViewItem.ListViewSubItem si = list.SubItems.Add("Hi");
si.ForeColor = Color.Red;

HTH

Kieran
 
T

timtos

You again omit some information but perhaps this can help you.
With the code below you can fill your content array as you like and
directly assign a background and a foreground color to your content.

After doing so, you set the number of columnheaders you like.
The listview is then filled always from left to right from top to bottom.

This is of course just a minimal version but I think it is easy to expand
this code to be a bit more sophisticated ;-)

private void Form1_Load(object sender, System.EventArgs e)
{
// 1. Prepare content somehow
int contentCount = 12;
Content[] content = new Content[contentCount];
content[0] = new Content("the cat", Color.Black, Color.Aquamarine);
content[1] = new Content("jumped", Color.CornflowerBlue, Color.Wheat);
content[2] = new Content("over", Color.Black, Color.Beige);
content[3] = new Content("the wall", Color.CornflowerBlue, Color.Aquamarine);
content[4] = new Content("the cat", Color.Black, Color.Aquamarine);
content[5] = new Content("jumped", Color.Black, Color.Beige);
content[6] = new Content("over", Color.Yellow, Color.Blue);
content[7] = new Content("the wall", Color.CornflowerBlue, Color.Red);
content[8] = new Content("jumped", Color.CornflowerBlue, Color.Red);
content[9] = new Content("the dog", Color.SeaGreen, Color.DarkViolet);
content[10] = new Content("the bird", Color.YellowGreen, Color.SaddleBrown);
content[11] = new Content("the fox", Color.Black, Color.White);

// Number of cells you want
// = Number of Column Headers
int cellCount = 3;

// Create and add items
for (int i=0; i<contentCount; i=i+cellCount)
{
ListViewItem lvi = new ListViewItem(content.text);
lvi.UseItemStyleForSubItems = false;
for (int c=1; c<cellCount; c++)
lvi.SubItems.Add(content[i+c].text, content[i+c].c1, content[i+c].c2, new Font("Arial", 8));
this.listView1.Items.Add(lvi);
}
}

Hope this helps!
Greetings,
timtos.

Patrick De Ridder said:
Below a rather simplistic example of a list view routine.
Say I want to have "over" appearing in red on the
display, how would I do that?
Many thanks,
Patrick.
(e-mail address removed)
***************************
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
\namespace WindowsApplication1
{
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.ListView listView1;
private System.Windows.Forms.ColumnHeader columnHeader1;
private System.Windows.Forms.ColumnHeader columnHeader2;
private System.ComponentModel.Container components = null;
public Form1()
{
InitializeComponent();
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region
#endregion
static void Main()
{
Application.Run(new Form1());
}
private void Form1_Load(object sender, System.EventArgs e)
{
string [] content = new string [4];
string [] show = new string [2];
content [0] = "the cat";
content[1] = "jumped ";
content [2] = "over";
content[3] = "the wall";
int k = -1;
for (int i=0 ;i<2;i++)
{
show [0] = content[++k];
show[1] = content [++k];
ListViewItem list = new ListViewItem(show);
listView1.Items.Add(list);
}
}
}
}
 
P

Patrick De Ridder

The correct spelling of the property is:
list.UseItemStyleForSubItems = false

Once this is set you can set individual columns in a row to different
styles.

e.g.

ListViewItem list = listView1.Items.Add( "item" + i.ToString() );
list.UseItemStyleForSubItems = false;
ListViewItem.ListViewSubItem si = list.SubItems.Add("Hi");
si.ForeColor = Color.Red;
<snipped>

Thank you very much.I will study your posting.

For the time being I have done it like this.
(Which is a very simple solution.)

(The lines fit into the example that I have posted above.)

ListViewItem list = new ListViewItem(show);
list.ForeColor = Color.Black;
if(some condition) list.ForeColor = Color.Red;
listView1.Items.Add(list)

It colours the whole line, which is not 100% what I wanted.

I will have a good look at your suggestions now!

Thank you again.
 
P

Patrick De Ridder

You again omit some information but perhaps this can help you.
With the code below you can fill your content array as you like and
directly assign a background and a foreground color to your content.
<snipped>

***************************************************
<< I had sent you an email as well >>
***************************************************

Thank you very much.I will study your posting.

For the time being I have done it like this.
(Which is a very simple solution.)

(The lines fit into the example that I have posted above.)

ListViewItem list = new ListViewItem(show);
list.ForeColor = Color.Black;
if(some condition) list.ForeColor = Color.Red;
listView1.Items.Add(list)

It colours the whole line, which is not 100% what I wanted.

I will have a good look at your suggestions now!

Thank you again.
 
P

Patrick De Ridder

The correct spelling of the property is:
list.UseItemStyleForSubItems = false

Once this is set you can set individual columns in a row to different
styles.

e.g.

ListViewItem list = listView1.Items.Add( "item" + i.ToString() );
list.UseItemStyleForSubItems = false;
ListViewItem.ListViewSubItem si = list.SubItems.Add("Hi");
si.ForeColor = Color.Red;

Hi Kieran,
Your code works excellently. Thank you very much again.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top