Làm cách nào để đặt màu nền của một mục cụ thể trong System.Windows.Forms.ListBox? Tôi muốn có thể thiết lập nhiều cái nếu có thể.Màu nền của một mục ListBox (winforms)
Trả lời
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)
// 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;
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.
Tuyệt vời, bit được chọn rất hữu ích. – Almo
Báo cáoForegroundBrushSelected: reportsForegroundBrush ?? –
reportsForegroundBrushSelected: reportsForegroundBrush cho tôi lỗi, chúng giả sử được khai báo nhưng làm cách nào? –
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
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);
}
- 1. Màu nền của ListBox (XAML/WinRT/Metro)
- 2. Winforms - Cách ngăn lựa chọn mục Listbox
- 3. Winforms, databinding, Listbox và textbox
- 4. Cách tô màu các phần của đường Listbox/ListView trong C# WinForms?
- 5. Thay đổi màu Foreground của một ContentPresenter trong một ListBox
- 6. C màu nền # WinForms DataGridView render quá chậm
- 7. Làm thế nào để thay đổi màu nền lựa chọn ListBox?
- 8. WP7 ListBox Mục để điền vào chiều rộng của ListBox
- 9. Thay đổi màu nền của một NSCollectionView
- 10. Làm cách nào để thay đổi màu nền của điều khiển tab trong Winforms?
- 11. Nhận văn bản của mục đã chọn trong một ListBox
- 12. Màu nền của giao diện người dùng Jquery thay đổi màu nền trong một ngày
- 13. Màu nền của iPhone UIButton
- 14. Xóa mục khỏi ListBox?
- 15. Lấy màu nền của ImageView
- 16. Màu nền của một TextBox bị vô hiệu hóa
- 17. Tùy chỉnh phông chữ/màu nền của thanh chỉ mục/nền trong UITableView
- 18. màu eclipse: mục pref cho màu nền của tất cả các lần chọn là
- 19. Màu nền của mục đã chọn trong một không thể chỉnh sửa JComboBox
- 20. Mục đích của Console.WriteLine() trong Winforms
- 21. Cách đặt màu nền của một nút trong GUI Java?
- 22. Lấy màu nền của một nút trong android
- 23. Cách thay đổi màu nền tiêu đề của một QTableView
- 24. Thay đổi màu nền của một DateTimePicker trong .NET
- 25. Tại sao tôi không thể đặt màu nền của một ListBoxItem được chọn trong WPF?
- 26. Đầu ra của bảng điều khiển chuyển hướng đến winforms ListBox
- 27. Thay đổi màu nền của tab ActionBar
- 28. Màu nền biểu đồ của Google
- 29. Thay đổi màu nền của CGContext
- 30. Màu nền của tùy chọn đã chọn
Có thể với một ListBox. Xem http://stackoverflow.com/questions/91747/background-color-of-a-listbox-item-winforms#91758 – jfs
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. –
BackColor không phải là thuộc tính của mục 'ListBox.ObjectCollection' – ghiboz