You need to use a collection that supports change notifications for your data source. When you remove an item from a simple list, it does not tell anyone about it.
The following is an example of using a BindingList :
Public Class Form1 Private mycountries As New BindingList(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") ' BindingList doesn't have a Sort() method, but you can sort your data ahead of time 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) ' no need to set the DataSource again here 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
For your second question, you are not bound to a variable when adding a data binding. You are attached to an object (which acts as a data source), and then you specify a property on that object that will give you the value to which you want to bind.
So, for the button, you need something like this (apologies for C #, but you get this idea):
public class SomeModel { public bool ButtonEnabled { get; set; } } public class Form1 : Form { public Form1() { InitializeComponent(); SomeModel model = new SomeModel();
In general, data binding refers to a change notification. If you need to bind to user objects, look into the INotifyPropertyChanged interface.
source share