Back to Devexpress

ListBoxDrawItemEventArgs.Index Property

windowsforms-devexpress-dot-xtraeditors-dot-listboxdrawitemeventargs-23ed507a.md

latest5.7 KB
Original Source

ListBoxDrawItemEventArgs.Index Property

Gets the index of the item being painted.

Namespace : DevExpress.XtraEditors

Assembly : DevExpress.XtraEditors.v25.2.dll

NuGet Package : DevExpress.Win.Navigation

Declaration

csharp
public int Index { get; }
vb
Public ReadOnly Property Index As Integer

Property Value

TypeDescription
Int32

An integer value representing the zero-based index of the item being painted.

|

Remarks

Use the Index property to obtain the processed item’s index. It can be useful, for instance, when you need to custom paint an individual item.

You can also use the event parameter’s ListBoxDrawItemEventArgs.Item and ListBoxDrawItemEventArgs.State properties to obtain the item’s value and state.

Example

The following sample code handles the BaseListBoxControl.DrawItem event to custom paint odd and even list box items. The event handler also custom paints the currently selected item.

csharp
using DevExpress.XtraEditors;

private void listBoxControl1_DrawItem(object sender, DevExpress.XtraEditors.ListBoxDrawItemEventArgs e) {
    Brush evenItemBackBrush = Brushes.WhiteSmoke;
    Brush oddItemBackBrush = Brushes.LightBlue;
    Brush selectedItemBackBrush = Brushes.SteelBlue;
    string itemText = (sender as ListBoxControl).GetItemText(e.Index);
    if ((e.State & DrawItemState.Selected) != 0) {
        e.Cache.FillRectangle(selectedItemBackBrush, e.Bounds);
        using (Font f = new Font(e.Appearance.Font.Name, e.Appearance.Font.Size, FontStyle.Bold)) {
            e.Cache.DrawString(itemText, f, Brushes.White, e.Bounds, e.Appearance.GetStringFormat());
        }
        e.Handled = true;
        return;
    }
    if (e.Index % 2 == 0) {
        e.Cache.FillRectangle(evenItemBackBrush, e.Bounds);
    }
    else {
        e.Cache.FillRectangle(oddItemBackBrush, e.Bounds);
    }
    e.Cache.DrawString(itemText, e.Appearance.Font, Brushes.Black, e.Bounds, e.Appearance.GetStringFormat());
    e.Handled = true;
}
vb
Private Sub ListBoxControl1_DrawItem(sender As Object, e As DevExpress.XtraEditors.ListBoxDrawItemEventArgs) Handles ListBoxControl1.DrawItem
    Dim evenItemBackBrush As Brush = Brushes.WhiteSmoke
    Dim oddItemBackBrush As Brush = Brushes.LightBlue
    Dim selectedItemBackBrush As Brush = Brushes.SteelBlue
    Dim itemText As String = (TryCast(sender, ListBoxControl)).GetItemText(e.Index)
    If (e.State And DrawItemState.Selected) <> 0 Then
        e.Cache.FillRectangle(selectedItemBackBrush, e.Bounds)
        Using f As Font = New Font(e.Appearance.Font.Name, e.Appearance.Font.Size, FontStyle.Bold)
            e.Cache.DrawString(itemText, f, Brushes.White, e.Bounds, e.Appearance.GetStringFormat())
        End Using
        e.Handled = True
        Return
    End If
    If e.Index Mod 2 = 0 Then
        e.Cache.FillRectangle(evenItemBackBrush, e.Bounds)
    Else
        e.Cache.FillRectangle(oddItemBackBrush, e.Bounds)
    End If
    e.Cache.DrawString(itemText, e.Appearance.Font, Brushes.Black, e.Bounds, e.Appearance.GetStringFormat())
    e.Handled = True
End Sub

The following code snippet (auto-collected from DevExpress Examples) contains a reference to the Index property.

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-listbox-item-templates-enable-word-wrap/CS/S131055/Form1.cs#L45

csharp
e.Appearance.DrawBackground(e.Cache, e.Bounds);
TextUtils.DrawString(e.Graphics, control.GetItemText(e.Index), control.Appearance.Font,
    control.Appearance.ForeColor, e.Bounds);

winforms-listbox-item-templates-enable-word-wrap/VB/S131055/Form1.vb#L44

vb
e.Appearance.DrawBackground(e.Cache, e.Bounds)
TextUtils.DrawString(e.Graphics, control.GetItemText(e.Index), control.Appearance.Font, control.Appearance.ForeColor, e.Bounds)
e.Handled = True

See Also

Item

State

DrawItem

ListBoxDrawItemEventArgs Class

ListBoxDrawItemEventArgs Members

DevExpress.XtraEditors Namespace