It's been a while since I've done Windows Forms coding, so I have to think about doing things that many of you who read this blog take for granted. Like adding values to a Windows Combobox. Hats off to Tomohiro who wrote a nifty little AddValue class to do just that.
Before showing the AddValue class in action, its a good time to look at the Album struct passed from the CS Photo Gallery Web Service.
public struct Album
{
public int ID;
public string Name;
public int PhotoCount;
public Album[] Albums;
}
Notice how each Album can contain an array of Albums to hold any sub-albums. The foreach{} to obtain all albums, including first tier sub-albums and populate the dropdown from Albums using Tomohiro's AddValue class would look like
ArrayList AlbumArray = new ArrayList();
foreach (CSMultiImageWindows.dbvtcs21.Album album in albums)
{
AlbumArray.Add(new AddValue(album.Name, album.ID));
if (album.Albums.Length > 1)
{
for (int i = 0; i < album.Albums.Length; i++)
{
// the [ i ] is written purposely for this post to avoid displaying as an emoticon...
AlbumArray.Add(new AddValue(" -- " + album.Albums[ i ].Name, album.Albums[ i ].ID));
}
}
}
this.cbAlbums.DataSource = AlbumArray;
this.cbAlbums.DisplayMember = "Display"
this.cbAlbums.ValueMember = "Value"
The AddValue class itself looks like so.
public class AddValue
{
private string _display;
private int _value;
public AddValue(string Display, int Value)
{
_display = Display;
_value = Value;
}
public string Display
{
get { return _display; }
}
public int Value
{
get { return _value; }
}
}