Dave Burke : Freelance .NET Web Developer specializing in Online Communities

CS Photo Galleries Writer Plugin: A Combo Box Sidebar

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; }
}
}

Comments (5) | Post RSS RSS comment feed

Posted on 9/6/2006 7:10:00 PM by Dave Burke
Categories: .NET | Community Server
Tags:

Related posts

Comments (5) -

9/6/2006 7:30:49 PM Permalink

Dave - are you using .NET 2.0 for this?

If so, you're better off making use of the Dictionary class.

Dictionary albums = new Dictionary();
foreach (CSMultiImageWindows.dbvtcs21.Album album in albums)
{
    albums[album] = album.Name;
    if (album.Albums.Length > 1)
    {
        for (int i = 0; i < album.Albums.Length; i++)
        {
            albums[album.Albums[ i ]] = " -- " + album.Albums[ i ].Name;
        }
    }
}

this.cbAlbums.DataSource = new BindingSource(albums, null);
this.cbAlbums.DisplayMember = "Value"
this.cbAlbums.ValueMember = "Key"

Now your combobox items actually point to the Album instances themselves.

Album selectedAlbum = (Album)cbAlbums.SelectedValue;

See my blog post from this time last year:

www.madprops.org/.../...-a-generic-Dictionary.aspx

ps. "Album" is one of those words that gets funnier the more you type it!

mabster |

9/6/2006 7:34:53 PM Permalink

Oops! There's a typo in that code. The Dictionary should be defined as Dictionary, not the other way around. Sorry 'bout that.

mabster |

9/6/2006 8:20:03 PM Permalink

That's smart.  Dang!  I hate being corrected within minutes of posting some code.  That's one of the reasons I don't do it much anymore. Smile  It kind of makes this post of mine unnecessary, doesn't it.  Pfff!  Yes, I like the Generic Dictionary approach.  Thanks, Mabster.

daveburke |

9/6/2006 8:29:12 PM Permalink

Your approach is still useful to those stuck back in .NET 1.1 (poor b*stards)! Smile

mabster |

9/6/2006 8:33:19 PM Permalink

And for us poor b*stards who still can't figure out Generics.

daveburke |


Powered by BlogEngine.NET 2.0.0.36
Theme by Dave Burke