为 List(Of String) 提供显示成员
显然,这些不是过滤器的集合,而是一个过滤器的标准或子句的集合:
I condensed the code in the question, but there are 14 fields that can be filtered and there are multiple filters that can be applied on one field.
对于每个字段的倍数,我不确定是否要单独存储它们,但将字段条件保持在一起。所以,如果你想为这些应用一个名字,一个类不仅可以做到这一点,而且可以帮助管理过滤器元素:
Public Class SuperFilter
Public Property Name As String
Public Property Elements As SortedList
Public ReadOnly Property FilterText As String
Get
Return GetFilterText()
End Get
End Property
Public Sub New(n As String)
Name = n
Elements = New SortedList
End Sub
Public Sub AddItem(filter As String)
Elements.Add(Elements.Count, filter)
End Sub
Public Sub InsetAt(index As Int32, filter As String)
Elements.Add(index, filter)
End Sub
Private Function GetFilterText() As String
Dim els(Elements.Count - 1) As String
Elements.Values.CopyTo(els, 0)
Return String.Join(" ", els)
End Function
Public Overrides Function ToString() As String
Return String.Format("{0} ({1})", Name, Elements.Count.ToString)
End Function
End Class
您需要添加诸如Remove 和Count 之类的方法和属性,但这应该足以演示。我不确定SortedList,使用字段名称的Dictionary 可能会更好,但控制顺序的东西似乎值得。我也不确定我是否会公开 Elements 集合 - 管理它可能最好留给班级。
希望显示一组这些(而不是过滤器元素/子句)的 Combo 是目标。
Private filters As New List(Of SuperFilter)
将过滤器项添加到列表中:
Dim item As New SuperFilter("Default")
item.AddItem("Id = 7")
filters.Add(item)
item = New SuperFilter("Blue Ones")
item.AddItem("Color = Blue")
filters.Add(item)
item = New SuperFilter("Complex")
item.AddItem("[Name] like %Bob% OR [Name] like %Alice%")
item.AddItem("AND Color = 'Blue'")
item.AddItem("AND Active=True")
item.AddItem("AND AccessRequired < 3")
item.AddItem("AND DateAdded > #2/11/2010#")
item.AddItem("AND CreatedBy = 'ziggy'")
filters.Add(item)
cbo1.DataSource = filters
cbo1.DisplayMember = "Name"
cbo1.ValueMember = "FilterText"
值成员可以是Elements - 过滤子句的集合,也可以是查询文本。 GetFilterText 方法将它们连接在一起,作为过滤器管理器类可以/应该做的一部分:
For n As Int32 = 0 To filters.Count - 1
Console.WriteLine("Name: {0} Count: {1}{2}Text:{3}", filters(n).Name,
filters(n).Elements.Count,
Environment.NewLine, filters(n).FilterText)
Next
结果:
名称:默认计数:1
文本:Id = 7
名称:Blue Ones 数量:1
文本:颜色 = 蓝色
名称:复杂计数:6
文本:[Name] like %Bob% OR [Name] like %Alice% AND Color = 'Blue' AND Active=True AND AccessRequired #2/11/2010# AND CreatedBy = 'ziggy'
如果您使用“元素”作为 ValueMember,您将取回集合。
组合显示用户的Name。在右侧,标签显示ValueMember,在这种情况下,它是FilterText 或加入Elements。正如我所说,您可以将实际集合取回为 SelectedValue,但这可以作为 SelectedItem 的一部分使用。
如果savable 意味着超出应用程序实例的生命周期,那就是另一个问题了,但这些很容易序列化。