2012-04-28 24 views
0

是存在於列表框顯示每個項目不同的TextFormat /樣式列表框AllowHTML在c#

實施例的方式:

數據1:
數據2:值世界
數據3:值你好

我試過這個,但我不能使下一個字符串以粗體顯示

private void lbDetails_DrawItem(object sender, DrawItemEventArgs e) 
    { 
     e.DrawBackground(); 

     Rectangle rec = e.Bounds; 

     string[] temp = lbDetails.Items[e.Index].ToString().Split(':'); 

     e.Graphics.DrawString(temp[0], 
      new Font("Arial",8, FontStyle.Regular), 
      Brushes.Black, 
      rec, 
      StringFormat.GenericDefault); 

     e.DrawFocusRectangle(); 
    } 

SOLUTION

private void lbDetails_DrawItem(object sender, DrawItemEventArgs e) 
    { 
     try 
     { 
      e.DrawBackground(); 

      string[] temp = lbDetails.Items[e.Index].ToString().Split(':'); 

      SizeF prevStr = e.Graphics.MeasureString(temp[0] + ": ", new Font("Arial", 8, FontStyle.Regular)); 
      SizeF nextStr = e.Graphics.MeasureString(temp[1], new Font("Arial", 8, FontStyle.Bold)); 

      RectangleF firstRec = new RectangleF(e.Bounds.X, e.Bounds.Y, prevStr.Width, prevStr.Height); 
      RectangleF secondRec = new RectangleF(prevStr.Width, e.Bounds.Y, nextStr.Width, nextStr.Height); 

      e.Graphics.DrawString(temp[0] + ": ", 
       new Font("Arial", 8, FontStyle.Regular), 
       Brushes.Black, 
       firstRec, 
       StringFormat.GenericDefault); 

      e.Graphics.DrawString(temp[1], 
       new Font("Arial", 8, FontStyle.Bold), 
       Brushes.Black, 
       secondRec, 
       StringFormat.GenericDefault); 

      //e.Graphics.DrawRectangle(Pens.Red, firstRec.X, firstRec.Y, firstRec.Width, firstRec.Height); 

      e.DrawFocusRectangle(); 
     } 
     catch (Exception ex) 
     { 

     } 
    } 
+0

你有沒有在這段代碼中設置一個斷點,並且斷點被擊中? – Steve 2012-04-28 20:25:02

+0

是的..它通常工作..像列表框中的每個項目越來越大膽..我的觀點是,如果項目是「Data3:價值你好」< - 只有「值你好」將在粗體狀態.. – 2012-04-28 20:29:09

+0

我認爲它不會工作,然後.. :)我只會找到另一種解決方案.. – 2012-04-28 20:35:50

回答

1

您需要繪製字符串,第二個用加粗字體。一個完整的例子:

public partial class Form1 : Form { 
    public Form1() { 
     InitializeComponent(); 
     listBox1.DrawMode = DrawMode.OwnerDrawFixed; 
     listBox1.DrawItem += new DrawItemEventHandler(listBox1_DrawItem); 
     listBox1.Items.AddRange(new object[] { "Data1: value", "Data2: hello world", "Data3: hello hello"}); 
    } 

    void listBox1_DrawItem(object sender, DrawItemEventArgs e) { 
     var box = (ListBox)sender; 
     e.DrawBackground(); 
     if (e.Index < 0) return; 
     string[] temp = box.Items[e.Index].ToString().Split(':'); 
     int size = (int)(TextRenderer.MeasureText(e.Graphics, temp[0], box.Font).Width + 0.5); 
     var rc = new Rectangle(e.Bounds.Left, e.Bounds.Top, (int)size, e.Bounds.Height); 
     var fmt = TextFormatFlags.Left; 
     TextRenderer.DrawText(e.Graphics, temp[0] + ":", box.Font, rc, e.ForeColor, fmt); 
     if (temp.Length > 1) { 
      using (var bold = new Font(box.Font, FontStyle.Bold)) { 
       rc = new Rectangle(e.Bounds.Left + size, e.Bounds.Top, e.Bounds.Width - size, e.Bounds.Height); 
       TextRenderer.DrawText(e.Graphics, temp[1], bold, rc, e.ForeColor, fmt); 
      } 
     } 
     e.DrawFocusRectangle(); 
    } 
} 

產地:

enter image description here

0

你可以做到這一點在本Link提。我知道你嘗試的方式不同,但它可以幫助你。

另外還有一個link。此鏈接描述如何以不同的字體樣式顯示ListBox中的項目。

Here is other good link as well.

+0

這是一樣的我的例如.. :) – 2012-04-28 20:47:29

+0

@vrynxzent:對冗餘抱歉 – jams 2012-04-28 20:50:07