For a long time now I have been trying to fix a problem with a combobox (it givs me headaches when I think at how many tests and forums I've tried). I know that after a combobox has been "binded" to a source it will be synchronised with it(every change will appear in combobox).
Here's my simple testing code:
Public Class Form1
Dim a As New BindingSource, b As New Hashtable
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
ComboBox1.DataSource = a
ComboBox2.DataSource = a
b.Add(1, "a")
b.Add(2, "b")
a.DataSource = b
a.DataMember = "Value"
'' Tried this when a.DataMember is commented .Otherwise it gives error
''ComboBox1.DisplayMember = "Value"
''ComboBox1.ValueMember = "Key"
''ComboBox2.DisplayMember = "Value"
''ComboBox2.ValueMember = "Key"
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
b.Add(3, "c") ''Nothing new in the combobox
''a.ResetBindings(False)
End Sub
End Class
This is how it looks:
When I de-comment the DisplayMember and ValueMember properties of the comboboxes this is what happens(and after pressing Button1):
As you can see the count property says there are 2 items but the DataSource says there are 3. I think that's why the new-added item doesn't appear (when I press Button1 the new element is not being added despite the fact it is stored in the BindingSource).
Where is the problem?
Note: I found something maybe useful on MSDN but it's not quite working (I updated the code according to this page).