内部组合框的数组访问组合框的属性 [英] Access combobox properties inside array of comboboxes
问题描述
我有正在使用Visual Studio 2010的动态添加到Windows窗体在VB.Net组合框的数组。
I have an array of combo boxes that are dynamically added to a windows form in VB.Net using Visual Studio 2010.
我想访问cboBox.list和cboBox.selecteditem属性,我绝对可以,如果我用
I would like to access the cboBox.list and cboBox.selecteditem properties, and I absolutely can if I use
dim cboList() as ComboBox = {}
ReDim Preserve cboList(cboList.Count)
Dim location As New System.Drawing.Point(FieldX, FieldY)
cboList(cboList.Count - 1) = New ComboBox
With cboList(cboList.Count - 1)
.Name = "cboName"
.Location = location
.Size = Size
.TabIndex = 1
End With
然后访问它,我请使用
And then to access it I use either
cboList(0).Items
或
cboList(0).SelectedIndex
由于这些从在数据库中,其中,我存储姓名,X / Y位置等,并且可以在当程序运行不同的时间被加入的信息产生的,我可以不具有在0位置相同的组合框每一次。我一直试图找到一种方式来做到这一点:
Because these are generated from information in the database, where I store their name, X/Y location, etc., and may be added at different times while the program is running, I may not have the same combobox in 0 location each time. I've been trying to find a way to do this:
cbolist("ComboName").Items
和都拿出了干在互联网上。有没有办法使用组合框的名称数组中找到找到正确的组合框的方式,如果是这样,怎么样?
And have come up dry on the internet. Is there a way to use the combobox name in the array to find the find the proper combobox, and if so, how?
我使用VB.Net框架3.5在Visual Studio 10。
I'm using VB.Net Framework 3.5 in Visual Studio 10.
推荐答案
是的,你需要使用词典(串,组合框)
而不是数组:
Yes, you need to use a Dictionary(Of String, ComboBox)
instead of an array:
Dim cboDictionary as Dictionary(Of String, ComboBox)
Dim cbo As New ComboBox
With cbo
'...
End With
cboDictionary.Add(cbo.Name, cbo)
这样做也将得到更高的性能,如果你有很多组合框,其数量有经常的变化的通知。这是因为词典
针对这种情况进行了优化,查找
,添加
和删除
业务在大多数情况下,在所有运行的 O(1)的时间。
Notice by doing so you would also get increased performance, if you have many ComboBoxes, and their number is changing often. This is because Dictionary
is optimized for this scenario, lookup
, add
and remove
operations in most cases all run in O(1) time.
数组没有查找
功能,因此它需要迭代找到一个元素,一个新的阵列需要创建添加或移除元素(您正在使用使用ReDim
)做的事情。相比之下,词典
做的不可以每次重新本身。
An array does not have lookup
functionality, so it would require iteration to find an element, and a new array needs to be created to add or remove an element (which you are doing with ReDim
). In contrast, Dictionary
does not recreate itself every time.
这篇关于内部组合框的数组访问组合框的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!