You want to use the DataSource property for data binding on a Windows Forms control. Bind an array to a ListBox on the screen, displaying all the strings. As changes are made to the List, you need to update the control on the screen. Here we see a simple example of binding a List to a DataSource, using the C# programming language.
First, your ListBox control, or any different kind of control you may have, has a DataSource property that can be assigned a value. You can assign a collection to it. Collections include arrays, Lists, and DataTables. The code that follows here is the first step.
public partial class MainWindow : Form
{
List<string> _sideList = new List<string>();
public MainWindow()
{
InitializeComponent();
sideListBox1.DataSource = _sideList;
}
}Description of the example code. It has a List collection field. The above Form, which is called MainWindow, has a member variable of the List generic type. This will be where we want to store the strings that will show on the ListBox.
(See ListBox in Windows Forms.)
Description of constructor. It has some routine setup code. The constructor and the partial class and InitializeComponent were autogenerated by the Designer and aren't really important here.
Using DataSource in the example. It assigns a DataSource. This is the important part here. The DataSource property is assigned to the List member variable.
The whole point here is that we want to add items to the ListBox. We can add items to the bottom or the top, or anywhere in better. A really good effect is adding items to the top, so they are easily seen. Here is a custom method that prepends a string to the top of the control, and then makes it display.
/// <summary>
/// Add the specified string to the very first row of the ListBox.
/// Then use CurrencyManager to force the rows to display again.
/// </summary>
/// <param name="lineIn">The string you want in the top row.</param>
private void AddListLine(string lineIn)
{
// Insert the string at the front of the List.
_sideList.Insert(0, lineIn);
// Force a refresh of the ListBox.
((CurrencyManager)sideListBox1.BindingContext[_sideList]).Refresh();
}Explanation of the above method. The method receives a string and then Inserts it to the start of the member List, and then forces the ListBox to refresh.
'Currency' here refers to current, meaning it is used to ensure a control is current. The syntax at the end of the above example does a cast and a lookup to get the CurrencyManager and then Refreshes the ListBox.
So far we have seen the DataSource assignment and then the method AddListLine that inserts a string at the top of the ListBox. This setup allows us to simply send a string to the method and it will show up at the top of the ListBox, which is really neat.
void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
// Show some results from the BackgroundWorker task.
AddListLine("5000 games: " + (string)e.Result);
}Here we used the DataSource property on a ListBox control in Windows Forms. By combining one List, DataSource and ListBox, and using CurrencyManager, we have a cool ListBox that will show the newest result on the top. This is far faster and simpler than dealing with TextBoxes. Together, these properties result in an interface that is versatile and easily maintained.