2008-09-18 70 views

Trả lời

50

Có lẽ cách duy nhất để thực hiện điều đó là tự vẽ các vật phẩm.

Đặt DrawMode để OwnerDrawFixed

và mã một cái gì đó như thế này trên các sự kiện DrawItem:

private void listBox_DrawItem(object sender, DrawItemEventArgs e) 
{ 
    e.DrawBackground(); 
    Graphics g = e.Graphics; 

    g.FillRectangle(new SolidBrush(Color.Silver), e.Bounds); 

    // Print text 

    e.DrawFocusRectangle(); 
} 

tùy chọn thứ hai sẽ được sử dụng một ListView, mặc dù họ có một cách khác triển khai (không thực sự dữ liệu bị ràng buộc, nhưng linh hoạt hơn theo cách cột)

2
// Set the background to a predefined colour 
MyListBox.BackColor = Color.Red; 
// OR: Set parts of a color. 
MyListBox.BackColor.R = 255; 
MyListBox.BackColor.G = 0; 
MyListBox.BackColor.B = 0; 

Nếu ý bạn là gì bằng cách đặt nhiều backgroun d màu sắc được thiết lập một màu nền khác nhau cho từng hạng mục, điều này là không thể với một ListBox, nhưng IS với một ListView, với một cái gì đó như:

// Set the background of the first item in the list 
MyListView.Items[0].BackColor = Color.Red; 
+2

Có thể với một ListBox. Xem http://stackoverflow.com/questions/91747/background-color-of-a-listbox-item-winforms#91758 – jfs

+0

s/possible/easy /. Oh well. C# 1, novice 0. Tôi không làm việc nhiều với các phương pháp vẽ quá tải trước đây. –

+0

BackColor không phải là thuộc tính của mục 'ListBox.ObjectCollection' – ghiboz

52

Cảm ơn answer by Grad van Horck, nó hướng dẫn tôi trong đúng hướng .

Để hỗ trợ văn bản (không chỉ là màu nền) ở đây là mã hoàn toàn làm việc của tôi:

//global brushes with ordinary/selected colors 
private SolidBrush reportsForegroundBrushSelected = new SolidBrush(Color.White); 
private SolidBrush reportsForegroundBrush = new SolidBrush(Color.Black); 
private SolidBrush reportsBackgroundBrushSelected = new SolidBrush(Color.FromKnownColor(KnownColor.Highlight)); 
private SolidBrush reportsBackgroundBrush1 = new SolidBrush(Color.White); 
private SolidBrush reportsBackgroundBrush2 = new SolidBrush(Color.Gray); 

//custom method to draw the items, don't forget to set DrawMode of the ListBox to OwnerDrawFixed 
private void lbReports_DrawItem(object sender, DrawItemEventArgs e) 
{ 
    e.DrawBackground(); 
    bool selected = ((e.State & DrawItemState.Selected) == DrawItemState.Selected); 

    int index = e.Index; 
    if (index >= 0 && index < lbReports.Items.Count) 
    { 
     string text = lbReports.Items[index].ToString(); 
     Graphics g = e.Graphics; 

     //background: 
     SolidBrush backgroundBrush; 
     if (selected) 
      backgroundBrush = reportsBackgroundBrushSelected; 
     else if ((index % 2) == 0) 
      backgroundBrush = reportsBackgroundBrush1; 
     else 
      backgroundBrush = reportsBackgroundBrush2; 
     g.FillRectangle(backgroundBrush, e.Bounds); 

     //text: 
     SolidBrush foregroundBrush = (selected) ? reportsForegroundBrushSelected : reportsForegroundBrush; 
     g.DrawString(text, e.Font, foregroundBrush, lbReports.GetItemRectangle(index).Location); 
    } 

    e.DrawFocusRectangle(); 
} 

Trên đây thêm vào đoạn code nhất định và sẽ hiển thị các văn bản phù hợp cộng với điểm nhấn mục đã chọn.

+1

Tuyệt vời, bit được chọn rất hữu ích. – Almo

+0

Báo cáoForegroundBrushSelected: reportsForegroundBrush ?? –

+0

reportsForegroundBrushSelected: reportsForegroundBrush cho tôi lỗi, chúng giả sử được khai báo nhưng làm cách nào? –

0
 public Picker() 
    { 
     InitializeComponent(); 
     this.listBox.DrawMode = DrawMode.OwnerDrawVariable; 
     this.listBox.MeasureItem += listBoxMetals_MeasureItem; 
     this.listBox.DrawItem += listBoxMetals_DrawItem; 
    } 

    void listBoxMetals_DrawItem(object sender, DrawItemEventArgs e) 
    { 
     e.DrawBackground(); 
     Brush myBrush = Brushes.Black; 
     var item = listBox.Items[e.Index] as Mapping; 
     if (e.Index % 2 == 0) 
     { 
      e.Graphics.FillRectangle(new SolidBrush(Color.GhostWhite), e.Bounds); 
     } 
     e.Graphics.DrawString(item.Name, 
      e.Font, myBrush, e.Bounds, StringFormat.GenericDefault); 
     e.DrawFocusRectangle(); 
    } 

Toàn bộ mẫu

0
private void listbox1_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e) 
     { 
      e.DrawBackground(); 
      Brush myBrush = Brushes.Black; 
       var item = listbox1.Items[e.Index]; 
       if(e.Index % 2 == 0) 
       { 
        e.Graphics.FillRectangle(new SolidBrush(Color.Gold), e.Bounds); 
       } 


      e.Graphics.DrawString(((ListBox)sender).Items[e.Index].ToString(), 
       e.Font, myBrush,e.Bounds,StringFormat.GenericDefault); 
      e.DrawFocusRectangle(); 
     } 


public MainForm() 
     { 
      InitializeComponent(); 
      this.listbox1.DrawItem += new System.Windows.Forms.DrawItemEventHandler(this.listbox1_DrawItem); 
     } 
Các vấn đề liên quan