The correct and practical method so far is the one by Salmen.
In case the strings shown in dialog box are not perfectly nice, this method could be slightly improved.
First, you need to understand the simple fact: list boxes and combo boxes can populate
lists with objects of any type. When the objects are accessed, items should be cast to the type of the object added, as Salmen demonstrated. The only problem is string representation of the item. It is obtained by .NET from what the method
object.ToString
returns.
Hence, the general method is: to create appropriate type for the list item and, class or structure, and override its
ToString
method:
internal struct ItemHelper {
internal ItemHelper(TimeZoneInfo info) { this.fInfo = info; }
public override string ToString() { return FormatInfo(this.fInfo); }
internal TimeZoneInfo Info { get fInfo; }
private TimeZoneInfo fInfo;
private static FormatInfo(TimeZoneInfo info) {
return ;
}
}
Add items of this type to the list items and cast them back to
ItemHelper
when time zone info is needed.
—SA