VB.net Objectlistview 按数字范围过滤列

VB.net Objectlistview filtering column by range of numbers

我有一个对象列表视图,其中有一列数字从 -3000 到 10000。我需要对小于 2000 的任何值应用过滤器(这也应该包括所有负数)。我已经阅读了示例和帮助 (http://objectlistview.sourceforge.net/cs/filtering.html#filtering-label),但它是在 C# 中,我正在使用 VB.net。我通常可以计算出转换,但这个让我很困惑。

我有另一段代码使用函数而不是委托(在应用图像时),但我无法让它在这个过滤实例中工作。我也尝试过使用正则表达式,但我只是觉得因为我正在处理数字,所以我应该在没有正则表达式的情况下进行。

谁能告诉我一个数字范围在 VB.net 中的自定义过滤示例来帮助我解决这个问题?

谢谢!

这是我拼凑的一个例子:

当您点击 "Apply Filter" 时,它应该只会显示 Mary Swanson 和 Jiminy Cricket(身高都在 30 岁以下)。

这是我用来创建 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

这是我需要帮助的过滤器按钮背后的代码

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

人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

错误表明 IModelFilter 不是委托类型。我不知道我应该从函数中返回什么??你看到我用在眼镜柱上的成像器了吗?我试图使用相同的方法,但我从未将它用于 IModelFilter。感谢您的帮助!

将过滤器设置为新的 ModelFilter。 x 是传递给函数的对象,将其转换为您的 Personclass,然后按高度过滤。过滤器在处理每个人时基本上 returns True(保留它)或 False(过滤掉它)。

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