使用类对象创建集合

时间:2016-09-06 21:00:27

标签: excel vba excel-vba

我试图通过使代码更加多态来稍微改变我的代码。我有一个用户表单,其中有5个选项卡。每个选项卡都有3列,可在运行时动态放置控件。

当我开始构建此表单时,我希望巩固每个控件的所有顶部和左侧位置。

现在我有这样的变量。

Tab0StartFromTop
Tab1StartFromTop
Tab2StartFromTop
Tab3StartFromTop
Tab4StartFromTop

Tab0Col1Left
Tab0Col2Left
Tab0Col3Left
''you get the picture

我想做的是这样的事情

Dim TabAttributes(0 To 4) As Collection
Dim ColumnAttributes As clsColumn ''clsColumn is a class object with properties for both .Top and .Left
Dim ColumnAttributeCollection As Collection

''Load Up my initial data. Distance from top will be incremented as I add controls
Set ColumnAttributes = New clsColumn
ColumnAttributes.Top = StartFromTop
ColumnAttributes.Left = StartFromLeft

''load all 3 objects into the (0) zero index for tab0
ColumnAttributeCollection(1).Add ColumnAttributes
ColumnAttributeCollection(2).Add ColumnAttributes
ColumnAttributeCollection(3).Add ColumnAttributes

''Now stick it inside my TabAttributes
TabAttributes(0).add ColumnAttributeCollection

所以现在我可以像这样或以某种方式访问​​它。

For each blah..
    TabAttributes(MyControl.Tabindex).Column(MyControl.ColumnIndex).Top
    TabAttributes(MyControl.Tabindex).Column(MyControl.ColumnIndex).Left
Next

当我遍历每个控件时,我需要调整顶部整数,因为更多控件被添加到该特定选项卡上的特定列。

TabAttributes(MyControl.Tabindex).Column(MyControl.ColumnIndex).Top = _
     TabAttributes(MyControl.Tabindex).Column(MyControl.ColumnIndex).Top + 40

我希望这是有道理的。提前感谢您的建议。

2 个答案:

答案 0 :(得分:0)

我个人更喜欢在集合上使用数组。也许这样的事情对你有用:

Const lTop As Long = 0      'Use named variable to always work with Top attribute
Const lLeft As Long = 1     'Use named variable to always work with Left attribute

Dim lTabIndex As Long
Dim aTabColumns() As Variant
ReDim aTabColumns(0 To 4, 0 To 1)
    'The first dimension (0 to 4) is for your tab numbers
    'The second dimension (0 to 1) shows if you're working with the Top or Left attribute for that tab's column
    'Example use:   aTabColumns(1, lTop) = aTabColumns(1, lTop) + 40

'Example showing how to loop through the array and set initial values
For lTabIndex = LBound(aTabColumns, 1) To UBound(aTabColumns, 1)
    aTabColumns(lTabIndex, lTop) = StartFromTop
    aTabColumns(lTabIndex, lLeft) = StartFromLeft
Next lTabIndex

答案 1 :(得分:0)

最后使用字典使其正常工作

Dim TabAttributes As Dictionary
Dim ColumnAttributes As Dictionary
Dim Column1Info As clsColumn
Dim Column2Info As clsColumn
Dim Column3Info As clsColumn
Dim TabColumnIndex As Integer

Set Column1Info = New clsColumn
Set Column2Info = New clsColumn
Set Column3Info = New clsColumn
Set TabAttributes = New Dictionary
Set ColumnAttributes = New Dictionary

'set column offsets
Column1Info.Left = StartFromLeft
Column1Info.Top = StartFromTop
Column2Info.Left = StartFromLeft + Column1Width + ColumnGap
Column2Info.Top = StartFromTop
Column3Info.Left = StartFromLeft + Column1Width + ColumnGap * 2 + Column2Width
Column3Info.Top = StartFromTop

'add column offsets to class object
ColumnAttributes.Add 1, Column1Info
ColumnAttributes.Add 2, Column2Info
ColumnAttributes.Add 3, Column3Info

'add class object to each tab index
For TabColumnIndex = 0 To 4
    TabAttributes.Add TabColumnIndex, ColumnAttributes
Next

所以现在我可以得到这样的数据......

TabAttributes(MyControl.Tabindex)(MyControl.ColumnIndex).Top = 125

感谢帮助人员