Search code examples
vb.netobjectlistview

ObjectListView BarRenderer in VB.net


I'm getting closer and closer to understanding the ObjectListView. The big learning curve is trying to convert all the examples from C# to VB.net.

I have a working ObjectListView that I created for learning purposes.

I'm trying to create a BarRenderer that's based off of the height column. I ended up creating a sub that triggers off of the FormatRow. However, when the bar displays, they aren't as expected. I set Joe to 75 and Mary to 25. I would expect the bars to be 75% and 25% full respectively.

There's probably a way to do this like I did with the imagegetter but I'm not sure how. I left the code there as an extra example.

    Private Sub Button3_Click_1(sender As Object, e As EventArgs) Handles Button3.Click

    Dim LvLst As New List(Of Person)

    Dim LvItm As New Person With {.FirstName = "Joe",
                                  .LastName = "Blow",
                                  .Glasses = "Y",
                                  .Height = "75"}
    LvLst.Add(LvItm)

    Dim LvItm2 As New Person With {.FirstName = "Mary",
                                  .LastName = "Swanson",
                                  .Glasses = "N",
                                  .Height = "25"}
    LvLst.Add(LvItm2)

    ObjectListView3.View = View.Details

    Dim myImages = New ImageList
    myImages.Images.Add(My.Resources.Hipster_Glasses_icon)
    myImages.Images.Add(My.Resources.Button_important_icon)
    ObjectListView3.SmallImageList = myImages

    ObjectListView3.OwnerDraw = True
    Col_Glasses.ImageGetter = Function(x As Object) As Integer
                                  Dim casted As Person = DirectCast(x, Person)
                                  If casted.Glasses = "Y" Then
                                      Return 0
                                  Else
                                      Return 1
                                  End If
                              End Function

    ObjectListView3.SetObjects(LvLst)

End Sub

Private Sub lsv_OpenTickets_FormatRow2(sender As Object, e As FormatRowEventArgs) Handles ObjectListView3.FormatRow
    Dim tkt As Person = DirectCast(e.Model, Person)
    Col_Height.Renderer = New BarRenderer(tkt.Height, 100, Pens.Black, Brushes.Gold)
End Sub

enter image description here

I've been using this site as a reference: http://objectlistview.sourceforge.net/cs/ownerDraw.html#owner-draw-label


Solution

  • Got it!

    I was trying to feed the value to the BarRenderer when in actuality, it automatically reads the value. You just supply the min and max for the bar and it creates it.

    Col_Height.Renderer = New BarRenderer(0, 100, Pens.Black, Brushes.Gold)

    In fact, you don't even need the FormatRow event:

    Private Sub Button3_Click_1(sender As Object, e As EventArgs) Handles Button3.Click
    
        Dim LvLst As New List(Of Person)
    
        Dim LvItm As New Person With {.FirstName = "Joe",
                                      .LastName = "Blow",
                                      .Glasses = "Y",
                                      .Height = "75"}
        LvLst.Add(LvItm)
    
        Dim LvItm2 As New Person With {.FirstName = "Mary",
                                      .LastName = "Swanson",
                                      .Glasses = "N",
                                      .Height = "25"}
        LvLst.Add(LvItm2)
    
        ObjectListView3.View = View.Details
    
        Dim myImages = New ImageList
        myImages.Images.Add(My.Resources.Hipster_Glasses_icon)
        myImages.Images.Add(My.Resources.Button_important_icon)
        ObjectListView3.SmallImageList = myImages
    
        ObjectListView3.OwnerDraw = True
        Col_Glasses.ImageGetter = Function(x As Object) As Integer
                                      Dim casted As Person = DirectCast(x, Person)
                                      If casted.Glasses = "Y" Then
                                          Return 0
                                      Else
                                          Return 1
                                      End If
                                  End Function
    
        Col_Height.Renderer = New BarRenderer(0, 100, Pens.Black, Brushes.Gold)
    
        ObjectListView3.SetObjects(LvLst)
    
    End Sub