Fred
Fred

Reputation: 3

Combobox SeletedItem does not wok ( Winform c# 3.5)

Create a Form, add a combobox in it, then paste :

    private void Form1_Load(object sender, EventArgs e)
    {           
        TimeZoneInfo myZome = TimeZoneInfo.Local;

        comboBox1.DataSource = TimeZoneInfo.GetSystemTimeZones();
        comboBox1.SelectedItem = myZome;
    }

ComboBox will display only the first element, it does not care about selectedItem... this driving me nuts any help please ? Thanks in advance Fred

Upvotes: 0

Views: 308

Answers (1)

Hubi
Hubi

Reputation: 1629

The instance of your timezone object (myZome) is not in the zones list. Find the right one in the collection.

Try this:

private void Form1_Load(object sender, EventArgs e)
{
    ICollection<TimeZoneInfo> zoneList = TimeZoneInfo.GetSystemTimeZones();
    TimeZoneInfo myZone = zoneList.First<TimeZoneInfo>(t => t.Id == TimeZoneInfo.Local.Id);
    comboBox1.DataSource = zoneList;
    comboBox1.SelectedItem = myZone;
}

Upvotes: 3

Related Questions