1

私はこのコードを試しましたが、うまくいきません:

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: 緑で --- 赤で値をたとえば、青のグーグル。

どうすればできますか?

4

2 に答える 2

0

オーナーが描画できるように、リスト ボックスの DrawMode プロパティを変更しましたか?

listBox1.DrawMode = DrawMode.OwnerDrawFixed;
于 2012-10-13T13:03:34.900 に答える
0

私は自分のプロジェクトの1つでこれを行っています。上記のターンキーに従って、次の行があります。

 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 によって決定されます。これは、描画されるアイテムで定義された色に設定されます。

于 2012-10-13T13:39:28.877 に答える