This seems like an obvious thing to achieve, but I just can't figure it out. Let's say I have a list of strings. How do I bind it to a listbox so that the listbox updates as the data of the list changes? I'm using vb.net.
I've tried this so far. I've manage to display the data, but not change it:
Public Class Form1
Private mycountries As New List(Of String)
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
mycountries.Add("Norway")
mycountries.Add("Sweden")
mycountries.Add("France")
mycountries.Add("Italy")
mycountries.Sort()
ListBox1.DataSource = mycountries 'this works fine
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
mycountries.RemoveAt(0)
ListBox1.DataSource = mycountries 'this does not update
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
MsgBox(mycountries(0))
End Sub
End Class
I've also tried this:
ListBox1.DataBindings.Add("items", mycountries, "Item")
But items is readonly, so it doesn't work.
Also, if I want to bind the enabled property of a button to a boolean, how can I do that? I've tried this but I don't know what to add for the last parameter.
Dim b As Boolean = True
Button3.DataBindings.Add("Enabled", b, "")