Back to Devexpress

BaseCheckedListBoxControl.CustomSort Event

windowsforms-devexpress-dot-xtraeditors-dot-basecheckedlistboxcontrol-150f06f2.md

latest4.6 KB
Original Source

BaseCheckedListBoxControl.CustomSort Event

Enables you to sort list items in custom order.

Namespace : DevExpress.XtraEditors

Assembly : DevExpress.XtraEditors.v25.2.dll

NuGet Package : DevExpress.Win.Navigation

Declaration

csharp
[DXCategory("Behavior")]
public event EventHandler<CheckedListBoxCustomSortEventArgs> CustomSort
vb
<DXCategory("Behavior")>
Public Event CustomSort As EventHandler(Of CheckedListBoxCustomSortEventArgs)

Event Data

The CustomSort event's data class is DevExpress.XtraEditors.Controls.CheckedListBoxCustomSortEventArgs.

Remarks

Handle the CustomSort event to compare two neighboring items (e.Item1 and e.Item2). You can compare items by their values (e.Value1, e.Value2) or display text (e.DisplayText1, e.DisplayText2). Set the e.Result parameter to 1 to position the first item above the second item, or set it to -1 to position the second item above the first item.

For optimization purposes, the editor itself does not raise the CustomSort event. Use the Sort() method to raise the CustomSort event when needed. For example, you can call this method within the ItemCheck event.

Note

  1. Custom sorting does not work in bound mode.
  2. The SortOrder property is ignored when you handle the CustomSort event.

Example

This example demonstrates how to handle the CustomSort event to sort the checked items first and then the rest of the items.

csharp
using DevExpress.XtraEditors.Controls;

private void checkedListBoxControl1_CustomSort(object sender, CheckedListBoxCustomSortEventArgs e) {
    if(e.Item1.CheckState == e.Item2.CheckState)
        e.Result = ((string)e.Value1).CompareTo((string)e.Value2);
    else
        e.Result = e.Item1.CheckState == CheckState.Checked ? -1 : 1;
}

private void checkedListBoxControl1_ItemCheck(object sender, DevExpress.XtraEditors.Controls.ItemCheckEventArgs e) {
    checkedListBoxControl1.Sort();
}

private void Form1_Load(object sender, EventArgs e) {
    checkedListBoxControl1.Items.AddRange(new CheckedListBoxItem[] {
        new CheckedListBoxItem(){ Value = "Bart Arnaz" },
        new CheckedListBoxItem(){ Value = "Leah Simpson" },
        new CheckedListBoxItem(){ Value = "Arnie Schwartz" },
        new CheckedListBoxItem(){ Value = "Billy Zimmer" },
        new CheckedListBoxItem(){ Value = "Samantha Piper" },
        new CheckedListBoxItem(){ Value = "Maggie Boxter" },
        new CheckedListBoxItem(){ Value = "Brad Farkus" },
    });

    checkedListBoxControl1.CheckOnClick = true;
}
vb
Imports DevExpress.XtraEditors.Controls

Private Sub checkedListBoxControl1_CustomSort(ByVal sender As Object, ByVal e As CheckedListBoxCustomSortEventArgs)
    If e.Item1.CheckState = e.Item2.CheckState Then
        e.Result = CStr(e.Value1).CompareTo(CStr(e.Value2))
    Else
        e.Result = If(e.Item1.CheckState = CheckState.Checked, -1, 1)
    End If
End Sub

Private Sub checkedListBoxControl1_ItemCheck(ByVal sender As Object, ByVal e As DevExpress.XtraEditors.Controls.ItemCheckEventArgs)
    checkedListBoxControl1.Sort()
End Sub

Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs)
    checkedListBoxControl1.Items.AddRange(New CheckedListBoxItem() {
        New CheckedListBoxItem() With {.Value = "Bart Arnaz"},
        New CheckedListBoxItem() With {.Value = "Leah Simpson"},
        New CheckedListBoxItem() With {.Value = "Arnie Schwartz"},
        New CheckedListBoxItem() With {.Value = "Billy Zimmer"},
        New CheckedListBoxItem() With {.Value = "Samantha Piper"},
        New CheckedListBoxItem() With {.Value = "Maggie Boxter"},
        New CheckedListBoxItem() With {.Value = "Brad Farkus"}
    })

    checkedListBoxControl1.CheckOnClick = True
End Sub

See Also

Sort()

BaseCheckedListBoxControl Class

BaseCheckedListBoxControl Members

DevExpress.XtraEditors Namespace