根据另一个文本框的值自动生成文本框值

时间:2017-11-06 09:23:52

标签: vb.net

我使用下面显示的方法从数据库中自动填充文本框:

    Dim comm As New SqlCommand("SELECT CustomerID from Table", 
    Database.Connection)
    Dim AutoComp As New AutoCompleteStringCollection
    Dim dsAccount As New DataSet
    Dim sqlAdapter As New SqlDataAdapter(comm)
    sqlAdapter.Fill(dsAccount)

    For i As Integer = 0 To dsAccount.Tables(0).Rows.Count - 1
        AutoComp.Add(dsAccount.Tables(0).Rows(i)(0).ToString())
    Next

    TextBox1.AutoCompleteMode = AutoCompleteMode.Suggest
    TextBox1.AutoCompleteSource = AutoCompleteSource.CustomSource
    TextBox1.AutoCompleteCustomSource = AutoComp

这很好用。我有一堆其他文本框,我希望在选择textbox1的值后填充适当的值。基本上,当我选择CustomerID时,我想要customerName,在这些其他文本框中生成分配给该特定CustomerID的contactName和customerAddress值。

Screenshot of the interface Auto-complete textbox

PS:我尝试使用相同的方法,但在按钮中而不是在Form_Load中使用它,但它没有用。所以我认为它不会那样工作或者我做错了。

1 个答案:

答案 0 :(得分:0)

如果你问的是如何回应用户从textbox1中的CustomerId自动完成列表中选择一个项目,那么你可以这样做:

Private Sub TextBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyDown

    If e.KeyCode = 13 Then

        'AN AUTOCOMPLETE ITEM HAS BEEN SELECTED    

        'The customerId will be available in textbox1.text

        'Your code to get  data from the database
        'and set the values of the other textboxes

    End If
End Sub