如何过滤vb中的组合框值

时间:2012-11-22 11:10:02

标签: combobox

我是VB的新手。我有一个政策号。 VB中的组合框,在我输入策略号的前四位后填充。但是,如果我继续输入数字,它会覆盖我已输入的内容并清除下拉列表,就像选择我输入的新4个数字一样。我想实现一个场景,我将输入前4个数字以填充下拉列表,因为我继续进入policyno下拉列表中的值被搜索。

EG:政策号码:969003648,当我输入9690时,下拉列表中填写了从969000001开始的策略号,现在当我继续输入值为969003等时,搜索到的值被限制为策略号没有969003作为起始值...请协助

我的代码:

Private Sub PolicyNo_KeyDown(KeyCode As Integer, Shift As Integer)

If KeyCode = 46 Or KeyCode = 8 Then
Me.PolicyNo.value = ""
Else

If Len(Me.PolicyNo.Text) >= 4  Then
Me.PolicyNo.RowSource = ""
Call ReloadPolicyNo(Nz(Me.PolicyNo.Text, ""))

Function ReloadPolicyNo(sPolicyNo As String)

Me.PolicyNo.RowSource = "SELECT Inventory.PolicyNo FROM Inventory " & _
                          "WHERE Left(Inventory.PolicyNo," & Len(Me.PolicyNo.Text) & ")  = '" & Me.PolicyNo.Text & "' order by Inventory.PolicyNo"
End Function

2 个答案:

答案 0 :(得分:0)

为什么不在组合框中使用Textchanged事件来获取当前组合框中的内容?

示例

    'THIS FIRES WHEN TEXT IS ENTERED INTO THE COMBOBOX
Private Sub ComboBox1_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox1.TextChanged

    'GET VALUE IN COMBOBOX
    Dim enteredText As String = ComboBox1.Text
    Dim strSelect As String = "select * from table where field like = '" & enteredText & "'%"

    'load COMBO BOX HERE

End Sub

答案 1 :(得分:0)

最好在这里使用DataView,我将DataTable声明为class Variable

Dim dt as DataTable

从数据库获取所有数据并将其绑定到声明的DataTable,此DataTable进一步添加到DataView并使用RowFilter过滤DataView。  Compay_Type是我DataTable中的列之一。下面的代码将过滤DataView并将数据绑定到列Company_Type=1

            Dim dvComp As New DataView(dt)
            dvComp.RowFilter = "Company_Type=1"
            ComboBox1.DisplayMember = "CompanyName"
            ComboBox1.ValueMember = "CompanyID"
            ComboBox1.DataSource = dvComp

要删除过滤器,请使用dvComp.RowFilter = Nothing