如何为ListBox中的特定项目中的特定文本着色?

时间:2012-10-13 12:47:05

标签: c#

我试过这段代码,但它不起作用:

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]);

结果如下:网址:http://www.google.com --- Localy KeyWord:google

现在,当试图用绿色对这条线进行着色时,颜色仍然是黑色,而listBox中的文字现在是:

GatherLinks.Form1 + MyListBoxItem很奇怪。

我想要做的是在listBox的第一行中着色Url:在红色中---蓝色和localykeyword:黄色 在第二行中,Url:绿色中的---红色,以及值为例如谷歌中的蓝色。

我该怎么做?

2 个答案:

答案 0 :(得分:0)

您是否更改了列表框的DrawMode属性以允许所有者绘图?

listBox1.DrawMode = DrawMode.OwnerDrawFixed;

答案 1 :(得分:0)

我在我的一个项目中这样做。按照上面的Turnkey,我有以下几行:

 tasksListBox.DrawMode = DrawMode.OwnerDrawFixed;
  tasksListBox.DrawItem += listBox_DrawItem;

在我的表单的初始化代码中。 DrawItem的代码处理选择颜色

之类的事情
private void listBox_DrawItem(object sender, DrawItemEventArgs e)
        {
            if (e.Index == -1)
            {
                return;
            }

            e.DrawBackground();
            Graphics g = e.Graphics;

            var selected = (e.State & DrawItemState.Selected) == DrawItemState.Selected;
            var lb = (ListBox)sender;

            if (selected)
            {
                g.FillRectangle(new SolidBrush(Color.DarkBlue), e.Bounds);
                g.DrawString(lb.Items[e.Index].ToString(), e.Font, new SolidBrush(Color.White), new PointF(e.Bounds.X, e.Bounds.Y));
                return;
            }

            var item = (ProjectToDoRecord)lb.Items[e.Index];
            var textColor = item.TrafficLight();
            g.FillRectangle(new SolidBrush(Color.White), e.Bounds);
            g.DrawString(lb.Items[e.Index].ToString(), e.Font, new SolidBrush(textColor), new PointF(e.Bounds.X, e.Bounds.Y));
        }

绘制的每个项目的颜色由textColor决定。这被设置为正在绘制的项目中定义的颜色。