将TimeZoneInfo添加到组合框并设置SeletedItem使我感到困惑 [英] Adding TimeZoneInfo to Combobox and setting SeletedItem baffles me

查看:73
本文介绍了将TimeZoneInfo添加到组合框并设置SeletedItem使我感到困惑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我要在所有计算机时区中填充一个组合框,然后默认选择本地时区。我可以填充组合框,但是选择默认项让我感到困惑。

So I am populating a combobox with all the computers time zones and then by default selecting the local time zone. I can populate the combobox but the selecting of the default item has me puzzled.

'load up the combobox with all time zones
Dim tzCollection As ReadOnlyCollection(Of TimeZoneInfo) = TimeZoneInfo.GetSystemTimeZones()
cboClockTmZone1.DataSource = tzCollection

'the computers local time zone
Dim myZone As TimeZoneInfo = TimeZoneInfo.Local

'get the index of the local time zone in the collections
Dim idx As Integer = tzCollection.IndexOf(myZone)
Dim qdx As Integer = cboClockTmZone1.Items.IndexOf(myZone)

'set the time zone
'cboClockTmZone1.SelectedIndex = idx 'this works
cboClockTmZone1.SelectedIndex = qdx 'this does not

所以也许我对此不太了解,但是当我将集合绑定到组合框时,TimeZoneInfo对象没有被填充。换句话说,不是每个TimeZoneInfo类型的列表项吗?如果是这样,为什么qdx分配不起作用?我返回-1,这意味着它无法在组合框中找到时区对象,但可以在tzCollection中找到它。我还尝试了.SelectedItem,但是也没有成功。

So maybe my lack of understanding this but when i bind the collection to the combobox does it not get filled with TimeZoneInfo objects. In other words isn't each list item of type TimeZoneInfo? If so why doesn't the qdx assignment not work? I get back a -1 which means it couldn't find the timezone object in the combobox but it can find it in the tzCollection. I also experimented with .SelectedItem but that didn't pan out either.

我只是不了解它应该如何工作。现在可以使用,因为我认为从tzCollection到cbo项之间存在一对一的映射。但是,如果有人可以对这两种方法有所了解,以便更好地理解,我将不胜感激。

I'm just not understanding how this is supposed to work. It works now because I think there is a one-to-one mapping from tzCollection to the cbo items. But if someone can shed some light on these two methods so I can understand better, I would appreciate it.

推荐答案

您正在运行进入TimeZoneInfo类中的错误。他们忘记重写Object.Equals()方法。该错误是已报告的错误,并且应在下一版.NET中修复。不太确定它是否能达到4.5,应该。

You are running into a bug in the TimeZoneInfo class. They forgot to override the Object.Equals() method. The bug is already reported and should be fixed in the next version of .NET. Not quite sure it makes it for 4.5, it should.

这篇关于将TimeZoneInfo添加到组合框并设置SeletedItem使我感到困惑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆