Back to Devexpress

BaseListBoxControl.CustomItemDisplayText Event

windowsforms-devexpress-dot-xtraeditors-dot-baselistboxcontrol-4f7e6e80.md

latest4.4 KB
Original Source

BaseListBoxControl.CustomItemDisplayText Event

Enables custom display text to be provided for control items.

Namespace : DevExpress.XtraEditors

Assembly : DevExpress.XtraEditors.v25.2.dll

NuGet Package : DevExpress.Win.Navigation

Declaration

csharp
[DXCategory("Behavior")]
public event CustomItemDisplayTextEventHandler CustomItemDisplayText
vb
<DXCategory("Behavior")>
Public Event CustomItemDisplayText As CustomItemDisplayTextEventHandler

Event Data

The CustomItemDisplayText event's data class is DevExpress.XtraEditors.CustomItemDisplayTextEventArgs.

Remarks

The CustomItemDisplayText event allows you to provide custom display text for control items. The CustomItemDisplayTextEventArgs object, passed to the event handler, exposes the following properties:

  • Item provides access to the item being processed;
  • Value contains the value of the current item. To provide custom display text for this value, you can format the value according to your needs and then assign the string to the DisplayText property;
  • Index gets the zero-based index of the item;
  • DisplayText allows you to customize the display text of the item being processed.

The code below illustrates how to replace RGB values with color names, if such values are present among KnownColors.

csharp
using DevExpress.XtraEditors;
using System.Drawing;

void OnCustomItemDisplayText(object sender, CustomItemDisplayTextEventArgs e) {
    // Create a Color from string values
    string[] colorValues = e.DisplayText.Split(',');
    for (int i = 0; i < colorValues.Length; i++) colorValues[i] = colorValues[i].Trim(' ');
    Color color = 
        Color.FromArgb(255, int.Parse(colorValues[0]), int.Parse(colorValues[1]), int.Parse(colorValues[2]));

    // Search for the RGB value in known colors, and return a color name if found
    bool colorFound = false;
    foreach (var colorValue in Enum.GetValues(typeof(KnownColor))) {
        Color knownColor = Color.FromKnownColor((KnownColor)colorValue);
        if (knownColor.A == color.A && knownColor.R == color.R && 
            knownColor.G == color.G && knownColor.B == color.B) {
                e.DisplayText = knownColor.Name + " (" + e.Value.ToString() + ")";
                colorFound = true;
                break;
        } 
    }
    if (!colorFound) e.DisplayText = "Unknown (" + e.Value.ToString() + ")";
}
vb
Imports DevExpress.XtraEditors
Imports System.Drawing

Private Sub OnCustomItemDisplayText(ByVal sender As Object, ByVal e As CustomItemDisplayTextEventArgs)
    ' Create a Color from string values
    Dim colorValues() As String = e.DisplayText.Split(","c)
    For i As Integer = 0 To colorValues.Length - 1
        colorValues(i) = colorValues(i).Trim(" "c)
    Next i
    Dim color As Color = Color.FromArgb(255, Integer.Parse(colorValues(0)), Integer.Parse(colorValues(1)), Integer.Parse(colorValues(2)))

    ' Search for the RGB value in known colors, and return a color name if found
    Dim colorFound As Boolean = False
    For Each colorValue In System.Enum.GetValues(GetType(KnownColor))
        Dim knownColor As Color = Color.FromKnownColor(CType(colorValue, KnownColor))
        If knownColor.A = color.A AndAlso knownColor.R = color.R AndAlso knownColor.G = color.G AndAlso knownColor.B = color.B Then
                e.DisplayText = knownColor.Name & " (" & e.Value.ToString() & ")"
                colorFound = True
                Exit For
        End If
    Next colorValue
    If Not colorFound Then
        e.DisplayText = "Unknown (" & e.Value.ToString() & ")"
    End If
End Sub

See Also

BaseListBoxControl Class

BaseListBoxControl Members

DevExpress.XtraEditors Namespace