Back to Devexpress

RepositoryItemComboBox.MeasureItem Event

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

latest6.6 KB
Original Source

RepositoryItemComboBox.MeasureItem Event

Allows you to change the height of specific items in the dropdown window.

Namespace : DevExpress.XtraEditors.Repository

Assembly : DevExpress.XtraEditors.v25.2.dll

NuGet Package : DevExpress.Win.Navigation

Declaration

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

Event Data

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

PropertyDescription
GraphicsGets the Graphics object to measure against.
IndexGets the index of the item for which the height and width is needed.
ItemHeightGets or sets the height of the item specified by the Index.
ItemWidthGets or sets the width of the item specified by the Index.

Remarks

The MeasureItem event fires for each item in the dropdown. It allows you to change the height of specific items with the ItemHeight event parameter. If you want to set the height for all items to a specific value, use the RepositoryItemComboBox.DropDownItemHeight property instead.

The MeasureItem event fires before an item is drawn. You can provide custom heights for items via the MeasureItem event and then, if required, custom paint the items by handling the ComboBoxEdit.DrawItem event.

Note

It is not possible to change an item width with the MeasureItem event, as the item width is controlled by the popup window width. See RepositoryItemPopupBase.PopupFormSize.

Example

The following example shows how to set a custom height for items in a ComboBoxEdit control by handling the RepositoryItemComboBox.MeasureItem event.

In this example, if a specific item contains a NewLine character (“\r\n” in C#), its height is doubled to fully display the item’s text.

csharp
using DevExpress.XtraEditors;
private void comboBoxEdit1_Properties_MeasureItem(object sender, MeasureItemEventArgs e) {
    ComboBoxEdit cb = sender as ComboBoxEdit;
    string itemValue = (string)cb.Properties.Items[e.Index];
    if (itemValue.Contains("\r\n"))
        e.ItemHeight = e.ItemHeight * 2;            
}
vb
Imports DevExpress.XtraEditors
Private Sub ComboBoxEdit1_Properties_MeasureItem(ByVal sender As System.Object, _
ByVal e As System.Windows.Forms.MeasureItemEventArgs) _
Handles ComboBoxEdit1.Properties.MeasureItem
    Dim cb As ComboBoxEdit = sender
    Dim itemValue As String = cb.Properties.Items(e.Index)
    If (itemValue.Contains(vbCrLf)) Then e.ItemHeight = e.ItemHeight * 2
End Sub

The following code snippet (auto-collected from DevExpress Examples) contains a reference to the MeasureItem 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-combobox-hide-specific-items/CS/MyComboBoxEdit/ComboBoxHideItemsHelper.cs#L33

csharp
_itemMeasure = new MeasureItemEventHandler(OnMeasureItem);
    _comboBox.Properties.MeasureItem += _itemMeasure;
}

winforms-combobox-hide-specific-items/VB/MyComboBoxEdit/ComboBoxHideItemsHelper.vb#L41

vb
_itemMeasure = New MeasureItemEventHandler(AddressOf OnMeasureItem)
    AddHandler _comboBox.Properties.MeasureItem, _itemMeasure
End If

See Also

ItemAutoHeight

DrawItem

DropDownItemHeight

PopupSizeable

PopupFormSize

RepositoryItemComboBox Class

RepositoryItemComboBox Members

DevExpress.XtraEditors.Repository Namespace