Search code examples
.netvb.netlistviewoverridingownerdrawn

How to use this overriding on betterlistview?


I've been working with BetterListView and I asked the devs if changing the color of the selection was possible. Here's their reply:

you can customize selection using Owner Drawing. This requires subclassing BetterListView and drawing custom selection instead of the default one:

public class CustomListView : BetterListView
{
    protected override void OnDrawItem(BetterListViewDrawItemEventArgs eventArgs)
    {
        if ((eventArgs.ItemStateInfo.ItemState & BetterListViewItemState.Selected) == BetterListViewItemState.Selected)
        {
            // draw custom selection
            eventArgs.Graphics.FillRectangle(Brushes.Brown, eventArgs.ItemBounds.BoundsSelection);
        }

        eventArgs.DrawSelection = false; // disable drawing default selection

        // default drawing (image, text etc.)
        base.OnDrawItem(eventArgs);
    }
}

Please note the eventArgs.ItemStateInfo.ItemState is a flags enum, i.e. the item can be hot, focused and selected independently and drawing of the custom selection may need to include these states for better user experience. The default selection uses Windows Theme so it is displayed the same way as in Windows Explorer.

Upon reading this I tried to understand how to use this overriding, but I never did such a thing. Despite, reading a lot about OwnerDrawing and Overriding, I am still confused.

Here's the code translated to VB.Net and the two other selection type added :

Imports ComponentOwl.BetterListView

Public Class CustomBetterListview

    Inherits BetterListView
    Protected Overrides Sub OnDrawItem(eventArgs As BetterListViewDrawItemEventArgs)

        If (eventArgs.ItemStateInfo.ItemState And BetterListViewItemState.Selected) = BetterListViewItemState.Selected Then
            ' draw custom selection
            eventArgs.Graphics.FillRectangle(Brushes.Brown, eventArgs.ItemBounds.BoundsSelection)
        End If

        If (eventArgs.ItemStateInfo.ItemState And BetterListViewItemState.Focused) = BetterListViewItemState.Focused Then
            ' draw custom selection
            eventArgs.Graphics.FillRectangle(Brushes.Red, eventArgs.ItemBounds.BoundsSelection)
        End If

        If (eventArgs.ItemStateInfo.ItemState And BetterListViewItemState.Hot) = BetterListViewItemState.Hot Then
            ' draw custom selection
            eventArgs.Graphics.FillRectangle(Brushes.Blue, eventArgs.ItemBounds.BoundsSelection)
        End If

        eventArgs.DrawSelection = False
        ' disable drawing default selection
        ' default drawing (image, text etc.)
        MyBase.OnDrawItem(eventArgs)
    End Sub

End Class

I suppose I'm on the right way, but I can't figure this out... it seems I'm missing a few fundamentals. I should probably mention that I'm trying to override BetterListViews placed on the form in design mode, as I suspect could possibly be a problem?


Solution

  • I've searched a bit about adding a custom control and it seems like rebuilding the project places the control at the top of the toolbox. Everything's working now.

    Thanks for helping out.