Use DataSource and data binding on a Windows Forms control. An array must be bound to a ListBox on the screen and will display all the strings. As changes are made to the data List, we need to update the control on the screen as well. We want a robust and simple solution.
First I need to show the very technique of the approach I am taking here. 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();
// Set the DataSource property of the ListBox called 'sideListBox1'
// to the List called '_sideList'.
sideListBox1.DataSource = _sideList;
}
// More stuff here was omitted.
}
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. Let me show 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();
}
The above method receives a string and then Inserts it to the start of the member List, and then uses some hocus-pocus to force the ListBox to refresh. This needs more explanation, so let me proceed with it.
This has nothing to do with money or exchange rates. '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 I have shown 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. Next is an example of how it is called.
void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
// Show some results from the BackgroundWorker task.
AddListLine("5000 games: " + (string)e.Result);
// See more about BackgroundWorker in my other article.
}
The code in this article will result in GUI somewhat similar to the screenshot. By combing 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.