By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,002 Members | 2,194 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,002 IT Pros & Developers. It's quick & easy.

How do I change the displayed text color of individual list box items?

P: 15
Hi I have a listbox that I am adding files to it. If the user checks a specific radiobutton when adding the item, I want to color the text with some color -- but only the text for that item, not all the items. The problem I am having is that my code colors all the items the same. Anyone know a simple way to do this?

The objects the user is adding to the listbox are files
and I want to display the Name of the file in the listbox, but I encounter two problems

1 - I cannot add an item with a color different from the other items in the listbox (they all have the same color)

2 - When drawing the text I cannot get the 'Name' property to be displayed. So if I add a custom class object to the list, all I see is the namespace.class name for all items.

Expand|Select|Wrap|Line Numbers
  1.  
  2. //Draw Item function
  3.  
  4.         private void listBox1_DrawItem(object sender, DrawItemEventArgs e)
  5.         {
  6.             e.DrawBackground();
  7.  
  8.             Graphics g = e.Graphics;
  9.             ListBox lb = (ListBox)sender;
  10.             g.DrawString(lb.Items[e.Index].ToString(), e.Font, new SolidBrush(radiobutton1.Checked==true?Color.Green:Color.Red), new PointF(e.Bounds.X, e.Bounds.Y)); 
  11.             e.DrawFocusRectangle();
  12.         }
  13.  
  14. //add items
  15.  
  16.  private void addButton_Click(object sender, EventArgs e) 
  17.         {
  18.                 //test.Substring(2, 5);
  19.                 int size = -1;
  20.                 // Show the dialog and get result.
  21.                 DialogResult result = openFileDialog1.ShowDialog();
  22.                 if (result == DialogResult.OK) // Test result.
  23.                 {
  24.                     string[] files = openFileDialog1.FileNames; //list of files selected
  25.  
  26.                     foreach (string file in files)
  27.                     {
  28.                         System.IO.FileInfo f = new System.IO.FileInfo(file);
  29.                         if (listBox1.FindString(f.Name) == -1)
  30.                         {
  31.  
  32.                 listBox1.DrawMode = DrawMode.OwnerDrawVariable;
  33.                                 listBox1.DrawItem += listBox1_DrawItem;
  34.                                 listBox1.Items.Add(f);
  35.                                 listBox1.DisplayMember = "Name";
  36.  
  37.                         }
  38.  
  39.                     }
  40.  
  41.                 }
  42. }
  43.  
Dec 18 '11 #1

✓ answered by Sal Sal


Share this Question
Share on Google+
1 Reply


P: 15

Post your reply

Sign in to post your reply or Sign up for a free account.