BaseListBoxControl.GetItemText(Int32) Method

Gets the text string displayed by the item.

Namespace: DevExpress.XtraEditors

Assembly: DevExpress.XtraEditors.v20.1.dll

Declaration

public virtual string GetItemText(
    int index
)
Public Overridable Function GetItemText(
    index As Integer
) As String

Parameters

Name Type Description
index Int32

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

Returns

Type Description
String

A string value representing the text displayed within the item. String.Empty if no item is found.

Remarks

The GetItemText method returns the item's text. If the control is not associated with the data source, the item's text is its value's string representation.

When the control is data bound, it displays the contents of the data source field whose name is specified by the BaseListBoxControl.DisplayMember property. If this property is not specified, the control displays values from a data source field whose name is specified by the BaseListBoxControl.ValueMember property. If this property is not specified, the item's type string representation is displayed.

If you need to obtain a specific item value, use the BaseListBoxControl.GetItemValue method.

Examples

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.

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;
}
See Also