winform checkedlistbox 設置行顏色

重寫OnDrawItem事件

public class ColorCodedCheckedListBox : CheckedListBox{
    protected override void OnDrawItem(DrawItemEventArgs e)
    {
         base.OnDrawItem(e);
    }
}    

  自定義時,需要重新聲明DrawItemEventArgs對象,然後調用base.OndrawItem方法。

DrawItemEventArgs e2 = new DrawItemEventArgs(e.Graphics, e.Font, new Rectangle(e.Bounds.Location, e.Bounds.Size), 
e.Index, (e.State & DrawItemState.Focus) == DrawItemState.Focus ? DrawItemState.Focus : DrawItemState.None, Color.Orange, this.BackColor);

如果想根據Item內容繪製不同樣式的Item時,只需要在該事件中訪問e.Index即可。

protected override void OnDrawItem(DrawItemEventArgs e)
        {
            if(e.Index%2==0)
            {
                DrawItemEventArgs e2 = new DrawItemEventArgs(e.Graphics, e.Font, 
new Rectangle(e.Bounds.Location, e.Bounds.Size),
e.Index, (e.State & DrawItemState.Focus) == DrawItemState.Focus ? DrawItemState.Focus : DrawItemState.None, Color.Orange, this.BackColor); base.OnDrawItem(e2); } else base.OnDrawItem(e); }

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章