BaseListBoxControl.GetItemText(Int32) Method
Gets the text string displayed by the item.
Namespace: DevExpress.XtraEditors
Assembly: DevExpress.XtraEditors.v19.1.dll
Declaration
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.
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 GetItemText(Int32) method.
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.