【问题标题】:VB.net Objectlistview filtering column by range of numbersVB.net Objectlistview 按数字范围过滤列
【发布时间】:2016-03-23 22:55:19
【问题描述】:

我有一个 objectlistview,其中有一列数字从 -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 不是委托类型。我不知道我应该从函数返回什么?你看到我用于眼镜柱的图像获取器了吗?我试图使用相同的方法,但我从未将它用于 IModelFilter。感谢您的帮助!

【问题讨论】:

  • 示例 C# 代码看起来很简单,应该很容易翻译成 vb.net。您能展示一下您尝试过的代码吗?
  • 嘿@ChaseRocker 我已按要求更新了问题。任何帮助将不胜感激
  • 你能贴出你的Person类的代码吗?
  • @chaserocker - 完成!

标签: vb.net objectlistview


【解决方案1】:

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

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

【讨论】:

  • 感谢@ChaseRocker!你为我节省了大量时间。对于使用此解决方案的其他人,我必须更改以使其工作的唯一另一件事是将 UseFiltering 设置为 True (ObjectListView3.UseFiltering = True)。
猜你喜欢
  • 1970-01-01
  • 2012-06-17
  • 2018-04-07
  • 2022-08-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-25
  • 1970-01-01
相关资源
最近更新 更多