内部组合框的数组访问组合框的属性 [英] Access combobox properties inside array of comboboxes

查看:142
本文介绍了内部组合框的数组访问组合框的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有正在使用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屋!

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