Back to Devexpress

RepositoryItemComboBox.DrawItem Event

windowsforms-devexpress-dot-xtraeditors-dot-repository-dot-repositoryitemcombobox-db85da3d.md

latest6.0 KB
Original Source

RepositoryItemComboBox.DrawItem Event

Provides the ability to custom paint the items displayed within the combo box editor’s drop down.

Namespace : DevExpress.XtraEditors.Repository

Assembly : DevExpress.XtraEditors.v25.2.dll

NuGet Package : DevExpress.Win.Navigation

Declaration

csharp
[DXCategory("Events")]
public event ListBoxDrawItemEventHandler DrawItem
vb
<DXCategory("Events")>
Public Event DrawItem As ListBoxDrawItemEventHandler

Event Data

The DrawItem event's data class is ListBoxDrawItemEventArgs. The following properties provide information specific to this event:

PropertyDescription
AllowDrawSkinBackgroundGets or sets whether the item background is colored according to the currently applied skin.
AppearanceGets the appearance settings used to paint the item currently being processed.
BoundsGets the bounding rectangle of the item being painted.
IndexGets the index of the item being painted.
ItemGets the value of the processed item.
PaintArgs
StateGets the state of the item being painted.

Remarks

Handle the DrawItem event to override the default item painting. This event is raised each time an item is about to be painted. The event parameter’s properties allow you to identify the processed item, its boundaries, state, appearance settings, etc. You must set the Handled property to true to override the default painting.

The editor’s ComboBoxEdit.DrawItem event is equivalent to the current event.

Example

The following sample code handles the ComboBoxEdit.DrawItem event to custom paint the currently selected item. The result is shown in the image below:

csharp
using System.Drawing.Drawing2D;
using DevExpress.XtraEditors;

private void comboBoxEdit1_DrawItem(object sender, ListBoxDrawItemEventArgs e) {
  if(e.State != DrawItemState.Selected) return;
    using(var backBrush = new LinearGradientBrush(e.Bounds, Color.Blue, Color.LightBlue,
            LinearGradientMode.Horizontal))
        e.Cache.FillRectangle(backBrush, e.Bounds);
    e.Cache.DrawString(e.Item.ToString(), e.Appearance.Font, Brushes.White,
        e.Bounds);
    e.Handled = true;
}
vb
Imports System.Drawing.Drawing2D
' ...
Private Sub ComboBoxEdit1_DrawItem(ByVal sender As Object, _
 ByVal e As DevExpress.XtraEditors.ListBoxDrawItemEventArgs) _
 Handles ComboBoxEdit1.DrawItem
    If e.State <> DrawItemState.Selected Then
        Return
    End If
    Using backBrush = New LinearGradientBrush(e.Bounds, Color.Blue, Color.LightBlue, LinearGradientMode.Horizontal)
        e.Cache.FillRectangle(backBrush, e.Bounds)
    End Using
    e.Cache.DrawString(e.Item.ToString(), e.Appearance.Font, Brushes.White, e.Bounds)
    e.Handled = True
End Sub

The following code snippet (auto-collected from DevExpress Examples) contains a reference 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.

winforms-imagecomboboxedit-disable-specific-items/CS/WindowsApplication33/Form1.cs#L33

csharp
imageComboBoxEdit1.Properties.DrawItem += new DevExpress.XtraEditors.ListBoxDrawItemEventHandler(Properties_DrawItem);
imageComboBoxEdit1.Properties.CloseUp += new CloseUpEventHandler(Properties_CloseUp);

winforms-imagecomboboxedit-disable-specific-items/VB/WindowsApplication33/Form1.vb#L29

vb
imageComboBoxEdit1.Properties.Items.Add(New ImageComboBoxItem("Cancel", 3))
AddHandler imageComboBoxEdit1.Properties.DrawItem, New ListBoxDrawItemEventHandler(AddressOf Properties_DrawItem)
AddHandler imageComboBoxEdit1.Properties.CloseUp, New CloseUpEventHandler(AddressOf Properties_CloseUp)

See Also

DropDownItemHeight

MeasureItem

RepositoryItemComboBox Class

RepositoryItemComboBox Members

DevExpress.XtraEditors.Repository Namespace