I am developing a Point of Sale software in Vb.net. I want to render the functionality which binds the Comboboxes and keep them synchronized. For eg: ComboBox1 displays the "Book_Name" and Combobox2 Displays the "Book_Code". As soon as the "Book_Name" is selected in Combobox1 by the user, Combobox2 must automatically display the corresponding "Book_Code". Can anyone suggest me a programming code to implement this functionality in VB.NET?
You would simply bind both controls to the same data source, eg
myBindingSource.DataSource = myDataTable
With ComboBox1
.DisplayMember = "BookName"
.ValueMember = "BookId"
.DataSource = myBindingSource
End With
With ComboBox2
.DisplayMember = "BookCode"
.ValueMember = "BookId"
.DataSource = myBindingSource
End With
You should bind both comboboxes to the same BindingSource instance. The BindingSource has its own DataSource property set to your DataTable instance or to your own class for a list of Books
Suppose you have a class for a Book defined as
Public Class Book
Public Property ID As Integer
Public Property Title As String
Public Property Code As String
End Class
Now your comboboxes could be binded in this way
' Initialize your data -> (or load from db a DataTable)
Dim bks = New List(Of Book)()
Dim b = New Book() With {.Code = "123", .Title = "ABC", .ID = 1}
bks.Add(b)
b = New Book() With {.Code = "456", .Title = "DEF", .ID = 2}
bks.Add(b)
' Create and initialize the BindingSource instance
Dim bs = New BindingSource()
bs.DataSource = bks
' Set the first combo to display the Title
cbo1.ValueMember = "ID"
cbo1.DisplayMember = "Title"
cbo1.DataSource = bs
' Set the second combo to display the code
cbo2.ValueMember = "ID"
cbo2.DisplayMember = "Code"
cbo2.DataSource = bs
Now the two comboboxes are synchronized and both display the same item because at the SelectedIndexChange event the BindingSource keeps them on the same record
This example uses for simplicity a custom class. If you want to use a DataTable then set the DataTable as the DataSource of the BindingSource and change the DisplayMember and ValueMember property of the two combos to the column name of your table.
This may work:
Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
Select Case ComboBox1.Text
Case "Book_Name"
ComboBox2.Text = "Book Code: 1982637"
Case "Something On ComboBox1"
ComboBox2.Text = "Something On ComboBox2"
'Etc.
End Select
End Sub
If the items of the two Comboboxes have the same order you could keep them synchronized by binding the selected index.
Like this:
Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
ComboBox2.SelectedIndex = ComboBox1.SelectedIndex
End Sub
Private Sub ComboBox2_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox2.SelectedIndexChanged
ComboBox1.SelectedIndex = ComboBox2.SelectedIndex
End Sub
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.