Google for "bind listbox" or "populate listbox" and all the search results are for binding to a database. What if we want to bind to a data structure in memory, such as an ArrayList, and have the ListBox automatically refresh when the ArrayList changes? Its actually a bit convoluted, but the results are worth it! Read on:
Example:
// Make a new ArrayListusing System.Collections;using System.Windows.Forms;class test { ArrayList myDataSource = new ArrayList(); ListBox list = new ListBox(); public test() { list.DataSource = myDataSource; } public void AddItem() { string newLogName = "Item " + (list.Items.Count); if (!list.Items.Contains(newLogName)) { // Helps in repopulating of combobox. BindingManagerBase bmb = this.BindingContext[mySource]; // Indicate that we are going to modify the data source bmb.SuspendBinding(); // Add our new item myDataSource.Add(newLogName); // Indicate that we are done modifing the data source bmb.ResumeBinding(); } else { MessageBox.Show("?"); return; } }}
The key step is the SuspendBinding() and ResumeBinding(). This is the magic that tells the system to refresh the listbox after we add or remove items from our datasource.
In this contrived example, we are putting an strings into the ArrayList, which are then displayed. The power of this method is when we have an array of objects. The ListBox will display each object's .ToString(). We can select items in the listbox and then refer to the underlying object.
For a more complex example on how to use this, see the WISP GUI on wisp.wikispaces.com. The Logging Manager displays a list of all the open log files, which are each objects.
To Bind a ComboBox to a Dictionary:
(From http://madprops.org/blog/Bind-a-ComboBox-to-a-generic-Dictionary/)
var choices = new Dictionary<string, string>(); choices["A"] = "Arthur"; choices["F"] = "Ford"; choices["T"] = "Trillian"; choices["Z"] = "Zaphod"; comboBox1.DataSource = new BindingSource(choices, null); comboBox1.DisplayMember = "Value"; comboBox1.ValueMember = "Key";
Now you can set comboBox1.SelectedValue = "F" and it will display "Ford"!
You can avoid the three ArgumentNullExceptions if you set the DataSource after creating the BindingSources:
BindingSource b = new BindingSource(); b.DataSource = choices; comboBox1.DataSource = b;//or if you're using C# 3.0 comboBox1.DataSource = new BindingSource() { DataSource = choices; }
Vista / Windows 7 has a distracting animation in the progress bar that isn't easy to disable.
http://www.codeproject.com/KB/progress/ProgBarPlus.aspx
Or, to simply dumb down the progress bar completely (C#):
Declare this in your class
[System.Runtime.InteropServices.DllImport("uxtheme.dll")]private static extern int SetWindowTheme(IntPtr hWnd, string pszSubAppName, string pszSubIdList);
And call from your form init:
SetWindowTheme(pBarMultiHand.Handle, " ", " ");