【问题标题】:Adding TimeZoneInfo to Combobox and setting SeletedItem baffles me将 TimeZoneInfo 添加到 Combobox 并设置 SelectedItem 让我感到困惑
【发布时间】:2012-04-02 10:26:54
【问题描述】:

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

'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 但也没有成功。

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

【问题讨论】:

    标签: vb.net collections combobox timezone selecteditem


    【解决方案1】:

    您在 TimeZoneInfo 类中遇到了错误。他们忘记了重写 Object.Equals() 方法。该错误是already reported,应该在.NET 的下一版本中修复。不太确定它是否适用于 4.5,它应该。

    【讨论】:

    • 所以除了我正在做的事情之外,还有其他更有利的解决方法吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-10-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多