BaseListBoxControl.DrawItem Event
Provides the ability to custom paint items displayed within the list box control.
Namespace: DevExpress.XtraEditors
Assembly: DevExpress.XtraEditors.v18.2.dll
Declaration
Event Data
The DrawItem event's data class is ListBoxDrawItemEventArgs. The following properties provide information specific to this event:
Property | Description |
---|---|
AllowDrawSkinBackground | Gets or sets whether the item background is colored according to the currently applied skin. |
Appearance | Gets the appearance settings used to paint the item currently being processed. |
Bounds | Gets the bounding rectangle of the item being painted. |
Cache | Gets an object which specifies the storage for the most used pens, fonts and brushes. |
Graphics | Gets an object used to paint an item. |
Handled | Gets or sets a value specifying whether default painting is required. |
Index | Gets the index of the item being painted. |
Item | Gets the value of the processed item. |
State | Gets the state of the item being painted. |
The event data class exposes the following methods:
Method | Description |
---|---|
DefaultDraw() | Performs default painting of an element. |
GetItemInfo() | This member supports the internal infrastructure, and is not intended to be used directly from your code. |
Remarks
The list box control enables you to custom paint its items. Handle the DrawItem event to override default item painting. This event is raised each time an item is about to be painted. The event parameter’s properties allow you to define the object used to paint, the item’s boundaries, style and the index of the item. To get the item’s state (selected, checked, grayed, inactive, etc) and the value of the item being drawn, use the ListBoxDrawItemEventArgs.State and ListBoxDrawItemEventArgs.Item properties respectively.
Note: you must set the ListBoxDrawItemEventArgs.Handled property to true to override the default painting.
Example
The following sample code handles the BaseListBoxControl.DrawItem
event to custom paint the items displayed within the ListBoxControl. The odd and even items are painted with different appearance settings. A specific painting is applied to the currently selected item.
The image below shows the result.
using DevExpress.XtraEditors;
private void listBoxControl1_DrawItem(object sender, ListBoxDrawItemEventArgs e) {
Brush backBrush1 = new SolidBrush(Color.FromArgb(224, 251, 254));
Brush backBrush2 = new SolidBrush(Color.FromArgb(198, 241, 249));
Brush backBrush3 = new SolidBrush(Color.FromArgb(253, 192, 47));
// declare field representing the text of the item being drawn
string itemText = (sender as ListBoxControl).GetItemText(e.Index);
if ((e.State & DrawItemState.Selected) != 0){
e.Cache.FillRectangle(backBrush3, e.Bounds);
ControlPaint.DrawBorder3D(e.Graphics, e.Bounds);
e.Cache.DrawString(itemText, new Font(e.Appearance.Font.Name,
e.Appearance.Font.Size, FontStyle.Bold), new SolidBrush(Color.Black),
e.Bounds, e.Appearance.GetStringFormat());
e.Handled = true;
return;
}
if(e.Index % 2 == 0){
e.Cache.FillRectangle(backBrush1, e.Bounds);
}
else{
e.Cache.FillRectangle(backBrush2, e.Bounds);
}
e.Cache.DrawString(itemText, e.Appearance.Font, new SolidBrush(Color.Black),
e.Bounds, e.Appearance.GetStringFormat());
e.Handled = true;
}
Related GitHub Examples
The following code snippets (auto-collected from DevExpress Examples) contain references to the DrawItem event.
Note
The algorithm used to collect these code examples remains a work in progress. Accordingly, the links and snippets below may produce inaccurate results. If you encounter an issue with code examples below, please use the feedback form on this page to report the issue.