473,405 Members | 2,354 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

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

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


1 2966

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

Similar topics

1
by: Margaret Asami | last post by:
I have 2 frames, one frame is a table of content with a bunch of links, and the other frame houses the contents. I want to be able to change the color of the link text when the user clicks on it. ...
14
by: Reply Via Newsgroup | last post by:
Folks, Say I have a table, ten columns, ten rows - Each with a word in it. I want to change the values of some/all of the cells in the table via a hyperlink. How do I reference each cell and...
3
by: Alpha | last post by:
Hello. I am not a C++ programmer...I have studied a bit of Java, but that's it. However, I am playing an online game written in C++. I was wondering how one could change the color of text (that...
5
by: AFN | last post by:
I'm trying to set a submit button to change text and color when clicked. Like saying "please wait" instead of "submit" and then changing the background color and text color. All works, except for...
0
by: Steven | last post by:
Hi, I have a toolbar control with several buttons in ToolBarButton class. How can I change the text color of the ToolBarButton control in VB.NET 2003? Thanks! Steven
1
by: Mohanapriya | last post by:
Hi, Is there a way to Open the excel in a specified path and modify the color of certain words/characters and save it the same path.
0
by: khanh | last post by:
I know how to highlight a treenode when the mouse is moving over it in a drag and drop, but what I would like to do is change the forecolor of a node and change it back when the mouse is moved away...
0
by: boysenberry | last post by:
Hi - I am using a sitemap to populate my Menu control. I need to be able to change the text of certain menu items at run time. Can this be done? I am using c# and asp.net. Any help would...
8
by: ismailc | last post by:
Hi, I would like to change the text color of (<xsl:value-of select="Description") onmouseover of image. the javascript works, it's just that i can seem to get the name correct <td>...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.