2016-09-06 4 views
0

私のコードをもう少し多態性にすることで、私のコードをちょっと変えようとしています。私は5つのタブがあるuserformを持っています。各タブには、実行時にコントロールを動的に配置する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 

のようなものです。複数のコントロールは、その特定のタブ上の特定の列に追加されるIトップ整数を調整する必要があります各制御を介してIループとしても

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 

私はそれが合理的であることを望みます。事前に感謝の意を表します。

+0

これを読んでいるところにはどこか質問がありませんでした。コントロール自体の '.Top'と' .Left'を使うことができない理由はありますか?私は、それらを複製することにどんな利益があるのか​​正確には分かりません。 – Comintern

+0

[VBAのカスタムコレクションクラス](http://www.cpearson.com/excel/CollectionClass.aspx)を参照してください。 – dee

答えて

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 
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 

助けを借りてくれてありがとう

関連する問題