私はこのコードを試しましたが、うまくいきません:
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]));
}
}
}
クラス MyListBoxItem は次のようになります。
public class MyListBoxItem
{
public MyListBoxItem(Color c, string m)
{
ItemColor = c; Message = m;
}
public Color ItemColor
{
get;
set;
}
public string Message
{
get;
set;
}
}
そして listBox1_DrawItem イベント:
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
}
}
色と描画アイテムでこれを試す前に、ListBoxで次の行を使用しました:
listBox1.Items.Add("Url: " + tokens[0] + " --- " + "Localy KeyWord: " + tokens[1]);
結果の例: URL: http://www.google.com --- Localy KeyWord: google
この行を緑で色付けしようとすると、色はまだ黒であり、listBox のテキストは次のようになります。
GatherLinks.Form1+MyListBoxItem がおかしい。
私がやりたかったのは、リストボックスの最初の行に色を付けることです: 赤で --- 青で localykeyword: 黄色で 2 行目に URL: 緑で --- 赤で値をたとえば、青のグーグル。
どうすればできますか?