【发布时间】: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