I tried this code but its not working:
private void LoadKeys(Dictionary<string,List<string>> dictionary, string FileName)
{
string line = System.String.Empty;
using (StreamReader sr = new StreamReader(keywords))
{
while ((line = sr.ReadLine()) != null)
{
string[] tokens = line.Split(',');
dictionary.Add(tokens[0], tokens.Skip(1).ToList());
listBox1.Items.Add(new MyListBoxItem(Color.Green, "Url: " + tokens[0] + " --- " + "Localy KeyWord: " + tokens[1]));
}
}
}
Now the class MyListBoxItem:
public class MyListBoxItem
{
public MyListBoxItem(Color c, string m)
{
ItemColor = c; Message = m;
}
public Color ItemColor
{
get;
set;
}
public string Message
{
get;
set;
}
}
And the listBox1_DrawItem event:
private void listBox1_DrawItem(object sender, DrawItemEventArgs e)
{
MyListBoxItem item = listBox1.Items[e.Index] as MyListBoxItem;
// Get the current item and cast it to MyListBoxItem
if (item != null)
{
e.Graphics.DrawString( // Draw the appropriate text in the ListBox
item.Message, // The message linked to the item
listBox1.Font, // Take the font from the listbox
new SolidBrush(item.ItemColor), // Set the color
0, // X pixel coordinate
e.Index * listBox1.ItemHeight // Y pixel coordinate. Multiply the index by the ItemHeight defined in the listbox.
);
}
else
{
// The item isn't a MyListBoxItem, do something about it
}
}
In the ListBox before trying this with the colors and draw item i used this line:
listBox1.Items.Add("Url: " + tokens[0] + " --- " + "Localy KeyWord: " + tokens[1]);
The result was for example: Url: http://www.google.com — Localy KeyWord: google
Now when trying to color it this line in Green the color is still black and the text in the listBox is now:
GatherLinks.Form1+MyListBoxItem strange.
What i wanted ot do is to color in the first line in the listBox the Url: in Red the — in blue and the localykeyword: in yellow
In the second line the Url: in Green the — in Red and the value for example google in Blue.
How can i do it ?
I am doing this in one of my projects. As per Turnkey above I have the lines:
In the initialization code of my form. The code for DrawItem deals with things like the selection color
The color of each item drawn is determined by textColor. This is set to a color defined in the item being drawn.