我有一个对象列表视图,其中一列数字范围从-3000到10000.我需要对小于2000的任何数据应用过滤器(这应包括所有负数)。我已经阅读了这些示例和帮助(http://objectlistview.sourceforge.net/cs/filtering.html#filtering-label),但它是在C#中,我正在使用VB.net。我通常可以计算出转换,但这个让我很难过。
我有另一段代码使用函数而不是委托(在应用图像时)但我无法让它在这个过滤实例中工作。我也尝试使用正则表达式,但我只是觉得,因为我正在处理数字我应该没有正则表达式。
有人可以在VB.net上向我展示一个带有数字范围的自定义过滤示例,以帮助我克服这个问题吗?
谢谢!
单击“应用过滤器”时,它应仅显示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
人类
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不是委托类型。我不知道我应该从函数返回什么?你看到我用于眼镜柱的imagegetter吗?我试图使用相同的方法,但我从未将它用于IModelFilter。谢谢你的帮助!
答案 0 :(得分:1)
将过滤器设置为新的ModelFilter。 x
是传递给函数的对象,将其强制转换为Person
类,然后按高度过滤。过滤器在处理每个Person时基本上返回True(保持它)或False(过滤掉)。
ObjectListView3.ModelFilter = New BrightIdeasSoftware.ModelFilter(Function(x) CType(x, Person).Height <= CInt(Me.HeightFilter.Text))