如何通过循环vb.net中的组合框项目在组合框上选择特定值

时间:2012-01-05 20:49:37

标签: vb.net combobox

我的vb.net应用程序中有一个表单,用于获取有关退回库存的数据。表格包含两个组合框。一个名为combobox5,包含发票号,另一个名为combobox3,包含聚会代码。两个组合框都使用sqldataadapter预加载。

现在我想要的是在combobox5中更改发票号时更改combobox3中的聚会代码。进一步说明,当发行股票时,聚会代码与发票号一起存储,以跟踪发行股票的当事方。现在当退回库存时,我想跟踪哪一方已退回库存,并且我希望在发票号更改时自动选择聚会代码,并且应该根据该特定发票号存储在数据库中。 ...

我正在使用以下代码:

    Private Sub ComboBox5_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox5.SelectedIndexChanged

    ' defines a new connection to the database
    Dim con As New SqlConnection("Data Source=TAHA;Initial Catalog=ADT;Integrated Security=True")
    con.Open()


    If ComboBox5.SelectedIndex = 0 Then


        ComboBox3.Enabled = True
        If Not ComboBox3.Items.Count = 0 Then

            ComboBox3.SelectedIndex = 0
        End If
    Else

        Me.ComboBox3.Enabled = False
        Me.ComboBox3.BackColor = Color.White
        Me.ComboBox3.ForeColor = Color.Black

        Dim invoices As New SqlCommand("select invoice_no, party_code from Outgoing_Invoice group by invoice_no, party_code", con)
        Dim reader As SqlDataReader = invoices.ExecuteReader

        While reader.Read

            Dim cnt, i As Integer

            cnt = Me.ComboBox3.Items.Count

            If Me.ComboBox5.SelectedItem.ToString.Trim = reader("invoice_no").ToString.Trim Then

                If Not cnt = 0 Then


                    For i = 0 To cnt - 1
                        If Me.ComboBox3.Items.Item(i).ToString.Trim.Contains(reader("party_code").ToString.Trim) Then   'here i have also used equals instead of contains but that too doesn't work
                            Me.ComboBox3.SelectedIndex = i
                            Exit For
                        End If
                    Next

                End If
            End If
        End While

        reader.Close()



    End If
    con.Close()


End Sub

1 个答案:

答案 0 :(得分:0)

您希望使用SelectionChangeCommitted事件,因为在以编程方式更改选择时也会触发SelectedIndexChanged。