1. 程式人生 > >C#中ListBox控制元件重繪Item項

C#中ListBox控制元件重繪Item項

一般情況下不需要手動重繪ListBox。

1、首先選中拖至面板的ListBox控制元件,點屬性,選中DrawMode,改成OwnerDrawFixed或OwnerDrawVariable
2、還是在屬性工具中,切換到事件(就是那個閃電圖示),滑鼠雙擊‘行為’選單下的DrawItem,新增一個事件。
3、在‘窗體設計器生成的程式碼’中就添加了一個新的事件

this.listBox1.DrawItem += new System.Windows.Forms.DrawItemEventHandler(this.listBox1_DrawItem);

4、學過的朋友應該知道,這時程式碼裡會有一個 listBox1_DrawItem()的方法,在裡面寫程式碼吧。


private void listBox1_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e)
{
      e.DrawBackground(); 
      Brush myBrush = Brushes.Black; //初始化字型顏色=黑色
      this.listBox1.ItemHeight=90; //設定項高,根據具體需要設定值      
      e.Graphics.DrawString(listBox1.Items[e.Index].ToString(), e.Font, myBrush,e.Bounds,null);
      //這句好象可以不要,自己試下
      e.DrawFocusRectangle();  
}


轉自:http://hi.baidu.com/lewutian/item/69d5f520d4b55517097508ac