开发者

Combobox SeletedItem does not wok ( Winform c# 3.5)

开发者 https://www.devze.com 2023-04-03 03:45 出处:网络
Create a Form, add a combobox in it, then paste : private void Form1_Load(object se开发者_Python百科nder, EventArgs e)

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

    private void Form1_Load(object se开发者_Python百科nder, 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


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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号