Search code examples
vb.netobjectlistview

VB.net Objectlistview filtering column by range of numbers


I have an objectlistview with a column of numbers ranging from -3000 to 10000. I need to apply a filter for anything less than 2000 (this should include all the negative numbers as well). I've read the examples and help (http://objectlistview.sourceforge.net/cs/filtering.html#filtering-label) but it's in C# and I'm working with VB.net. I can normally figure the conversion out but this one is stumping me.

I have another piece of code that uses a function instead of a delegate (when applying an image) but I couldn't get it to work in this filtering instance. I also tried using a regex but I just feel that since I'm dealing with numbers I should do it without regex.

Can someone please show me a custom filtering example with number ranges in VB.net to help me get over this?

Thanks!

Here is an example I threw together: enter image description here

When you click "Apply Filter", it should only show Mary Swanson and Jiminy Cricket (both under a height of 30).

Here is the code I used to create the olv

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",
                                  .HeightBar = "75"}
    LvLst.Add(LvItm)

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

    Dim LvItm3 As New Person With {.FirstName = "Mike",
                                    .LastName = "Tyson",
                                    .Glasses = "N",
                                    .Height = "125",
                                    .HeightBar = "125"}

    LvLst.Add(LvItm3)

    Dim LvItm4 As New Person With {.FirstName = "Jiminy",
                                    .LastName = "Cricket",
                                    .Glasses = "Y",
                                    .Height = "-9",
                                    .HeightBar = "-9"}

    LvLst.Add(LvItm4)


    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.UseCellFormatEvents = True
    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)

    'Set no data message
    ObjectListView3.EmptyListMsg = "No Data Found"
    ObjectListView3.EmptyListMsgFont = New Font("Tahoma", 18)

    'Allows you to type and search inside the olv
    ObjectListView3.IsSearchOnSortColumn = True

    ObjectListView3.SetObjects(LvLst)

End Sub

And this is the code behind the filter button that I need help on

Private Sub Button1_Click_1(sender As Object, e As EventArgs) Handles Button1.Click
    ObjectListView3.ModelFilter = Function(x As Object) As ModelFilter

                                      Dim casted As Person = DirectCast(x, Person)

                                      If casted.Height <= CInt(HeightFilter.Text) Then
                                          Return x
                                      End If

                                  End Function
End Sub

Person Class

Public Class Person
  Public Property FirstName As String
  Public Property LastName As String
  Public Property Glasses As String
  Public Property Height As Integer
  Public Property HeightBar As Integer
End Class

The error says that IModelFilter is not a delegate type. I don't know what i should be returning from the function?? Do you see the imagegetter I used for the glasses column? I was trying to use the same approach but I've never used it for a IModelFilter. Thanks for the help!


Solution

  • Set the filter to a new ModelFilter. x is the object being passed into the function, cast it to your Personclass, then filter by height. The filter basically returns True (to keep it) or False (to filter it out) as it processes each Person.

    ObjectListView3.ModelFilter = New BrightIdeasSoftware.ModelFilter(Function(x) CType(x, Person).Height <= CInt(Me.HeightFilter.Text))