2010-09-08 20 views

Trả lời

28

Bạn phải ghi đè lên các sự kiện Drawitem và thiết lập DrawMode tài sản để DrawMode.OwnerDrawFixed

kiểm tra mẫu này

private void listBox1_DrawItem(object sender, DrawItemEventArgs e) 
{ 
    if (e.Index<0) return; 
    //if the item state is selected them change the back color 
    if ((e.State & DrawItemState.Selected) == DrawItemState.Selected) 
     e = new DrawItemEventArgs(e.Graphics, 
            e.Font, 
            e.Bounds, 
            e.Index, 
            e.State^DrawItemState.Selected, 
            e.ForeColor, 
            Color.Yellow);//Choose the color 

    // Draw the background of the ListBox control for each item. 
    e.DrawBackground(); 
    // Draw the current item text 
    e.Graphics.DrawString(listBox1.Items[e.Index].ToString(),e.Font, Brushes.Black, e.Bounds, StringFormat.GenericDefault); 
    // If the ListBox has focus, draw a focus rectangle around the selected item. 
    e.DrawFocusRectangle(); 
} 

alt text

+0

Tôi đã quản lý để áp dụng mã này nhưng các mục trên hộp danh sách có màu trắng trên nền và nền trước. Tôi không thể thấy lý do tại sao nó sẽ làm điều đó? – Qosmo

+1

bạn có đặt thuộc tính 'DrawMode' thành' DrawMode.OwnerDrawFixed' không? – RRUZ

+1

Có, nếu không, nếu bình thường, nó là màu cửa sổ mặc định. – Qosmo

1

Mã dưới đây thực hiện chính xác những gì bạn đang nói:

Trong phương thức InitializeComponent:

this.listBox1.DrawMode = DrawMode.OwnerDrawFixed; 
this.listBox1.DrawItem += new System.Windows.Forms.DrawItemEventHandler(listBox1_DrawItem); 
this.listBox1.SelectedIndexChanged += new System.EventHandler(listBox1_SelectedIndexChanged); 

Và xử lý sự kiện:

void listBox1_SelectedIndexChanged(object sender, System.EventArgs e) 
{ 
    this.listBox1.Invalidate(); 
} 

void listBox1_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e) 
{ 
    int index = e.Index; 
    Graphics g = e.Graphics; 
    foreach (int selectedIndex in this.listBox1.SelectedIndices) 
    { 
     if (index == selectedIndex) 
     { 
      // Draw the new background colour 
      e.DrawBackground(); 
      g.FillRectangle(new SolidBrush(Color.Red), e.Bounds); 
     } 
    } 

    // Get the item details 
    Font font = listBox1.Font; 
    Color colour = listBox1.ForeColor; 
    string text = listBox1.Items[index].ToString(); 

    // Print the text 
    g.DrawString(text, font, new SolidBrush(Color.Black), (float)e.Bounds.X, (float)e.Bounds.Y); 
    e.DrawFocusRectangle(); 
} 

Mã được lấy từ:

http://www.weask.us/entry/change-listbox-rsquo-selected-item-backcolor-net

+0

Tôi đang gặp vấn đề với điều này. Việc lựa chọn thay đổi thành màu đỏ nhưng nó cũng nằm trên tất cả các mục mà tôi đã chọn trước đây, đánh bại mục đích có một mục "hình ảnh" được chọn. Nó có thể là gì? – Qosmo

6

Hy vọng rằng điều này sẽ giúp một người nào đó trong tương lai khi các mã trên đã giúp tôi nhưng không 100%

Tôi vẫn gặp sự cố sau:
- khi tôi chọn một chỉ mục khác, chỉ mục mới được chọn cũng sẽ làm nổi bật màu đỏ.
- khi tôi thay đổi kích thước phông chữ của hộp danh sách, khu vực được đánh dấu sẽ nhỏ.

Dưới bản sửa lỗi vấn đề

  • thay đổi các DrawMode để ownerdrawvariable
  • tạo MeasurItem và sự kiện DrawItem cho ListBox
private void lstCartOutput_MeasureItem(object sender, MeasureItemEventArgs e) 
{ 
    // Cast the sender object back to ListBox type. 
    ListBox listBox = (ListBox)sender; 
    e.ItemHeight = listBox.Font.Height; 
} 

private void lstCartOutput_DrawItem(object sender, DrawItemEventArgs e) 
{ 
    ListBox listBox = (ListBox)sender; 
    e.DrawBackground(); 
    Brush myBrush = Brushes.Black; 

    if ((e.State & DrawItemState.Selected) == DrawItemState.Selected) 
    { 
     myBrush = Brushes.Red; 
     e.Graphics.FillRectangle(new SolidBrush(Color.FromArgb(0, 64, 64)), e.Bounds); 
    } 

    else 
    { 
     e.Graphics.FillRectangle(Brushes.White, e.Bounds); 

    } 

    e.Graphics.DrawString(listBox.Items[e.Index].ToString(),e.Font, myBrush, e.Bounds); 
    e.DrawFocusRectangle(); 
} 


Tôi cũng tham khảo các trang web MSDN .

+0

Đây phải là câu trả lời đúng –

0

Tôi gặp vấn đề tương tự.

Thật không may nguồn dữ liệu của tôi là Danh sách lớp thực thể. Vì vậy, tôi có cùng mã với câu trả lời được chấp nhận ở trên nhưng với sửa đổi nhỏ để chọn thuộc tính chính xác trên Lớp của tôi mà tôi cần trên DrawString cho ListBox của mình:

if (e.Index < 0) return; 
     if ((e.State & DrawItemState.Selected) == DrawItemState.Selected) 
      e = new DrawItemEventArgs(e.Graphics, 
             e.Font, 
             e.Bounds, 
             e.Index, 
             e.State^DrawItemState.Selected, 
             e.ForeColor, 
             Color.Yellow); 

    e.DrawBackground(); 
    //This is my modification below: 
    e.Graphics.DrawString(ctListViewProcess.Items.Cast<entMyEntity>().Select(c => c.strPropertyName).ElementAt(e.Index), e.Font, Brushes.Black, e.Bounds, StringFormat.GenericDefault); 
    e.DrawFocusRectangle();