从Combobox中选择值并在文本框中显示相应的值

时间:2014-12-11 03:05:19

标签: vb.net combobox textbox

我有一个程序,我必须在组合框中选择一个值并自动将其对应的值显示到文本框中。

程序是这样的: 我将在combobox ='skucbo'中选择产品ID,然后产品描述将自动显示在textbox ='descriptiontxt'中。

这是我的代码:

Private Sub descriptiontxt_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles descriptiontxt.TextChanged

    Dim sku As String = skucbo.SelectedValue.ToString
    Dim dr As SqlDataReader
    connect()
    cmd = New SqlCommand
    cmd.CommandType = CommandType.Text
    cmd.Connection = con
    con.Open()
    cmd.CommandText = "SELECT description FROM tbl_sku WHERE skucbo = @sku"
    dr = cmd.ExecuteReader
    con.Close()
End Sub

希望得到你的回复。谢谢!

1 个答案:

答案 0 :(得分:0)

你还需要为你的组合框做一个SelectedIndexChanged 在SelectedIndexChanged中,您将文本框设置为组合框选定项。

“descriptiontxt.Text = skucbo.SelectedItem”

Private Sub skucbo_SelectedIndexChanged(sender As Object, e As EventArgs) Handles skucbo.SelectedIndexChanged
    descriptiontxt.Text = skucbo.SelectedItem
End Sub