简体   繁体   中英

Change Combobox via Textbox Input VB.NET

Hi I need some help over here... how do i change the display member of a ComboBox after i have entered a code in a textbox that it represent the combobox value??

example

Code: 02-001 Combobox: Provider X

if i change the code the provider combobox must change and if i change the provider combobox the code should change!.. i haven't found any help.. heres little code I remember

if e.keychar = chr(13) Then
    combobox.valuemember = textbox.text
    combobox.displaymember = me.stockdataset.selectprovider(@textbox.text)
end if

this code change the combo box display member but if I change the comobox by clicking it the code on the textbox doesnt change, to its corresponding code...?? please help

....the combo box is bound to the provider tables....

Change the selectedItem of your combobox when user press enter. But before, check that it is indeed in the provider list...

in the SelectionChanged handler of your combobox, set the textbox content to the value of the combobox.

Better each time use properties that uses propertychanged, and bind textbox and combobox's selectedItem to those properties.

This is wrong binding way....This is how you do it: I have simulate your case by a simple form with a combobox and a textbox and a datatable with 2 columns "Code" and "Description"

and here the Code (if you please, dont forget to mark my answer as Right ):

Public Class Form1

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try

            Me.KeyPreview = True'To Enable the Key events : KeyDown, KeyPress and KeyUp

            Dim dt As New DataTable("T")
            dt.Columns.Add(New DataColumn("Code"))
            dt.Columns.Add(New DataColumn("Description"))
            For i = 1 To 10
                Dim r As DataRow = dt.NewRow
                r("Code") = "Code" + CStr(i)
                r("Description") = "Desc" + CStr(i)
                dt.Rows.Add(r)
            Next

            With ComboBox1
                .ValueMember = "Code" ' This is case sensitive 
                .DisplayMember = "Description" ' This is case sensitive 
                .DataSource = dt
            End With

        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub TextBox1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.Click
        SetComoboValue()
    End Sub

    Private Sub TextBox1_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
        If e.KeyCode = Keys.Enter Then SetComoboValue()
    End Sub

    Private Sub SetComoboValue()
        Try
            If Not String.IsNullOrEmpty(TextBox1.Text) Then
                ComboBox1.SelectedValue = TextBox1.Text
            End If
        Catch ex As Exception
            MsgBox("Ooops, Invalid code !!!")
        End Try
    End Sub
End Class

So when you enter "Code3" and hit Enter or Click the TextBox1, the ComboBox1 will display "Desc3"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM