You are on page 1of 4

@@ -19,6 +19,38 @@ Public Items As New Collection

Public WithEvents owner As Form


Attribute owner.VB_VarHelpID = -1

Sub LoadSettingsFromTags(parent As Form)

Dim c As Control
Dim tmp() As String
Dim b(3) As Boolean

'note if .tag is missing elements


'the on error will let them default to false
'lazy programming but reliable..
On Error Resume Next

Set owner = parent

'Debug.Print "name | Tag | Boolean (order:


anchorTopLeft,fixedHeight,fixedWidth)"

For Each c In owner.Controls


'If c.name = "Text2" Then Stop 'to watch on Error default to false for
sure..
If Len(c.Tag) = 0 Then
b(0) = True
Else
tmp() = Split(c.Tag, ",")
For i = 0 To 2
b(i) = IIf(tmp(i) = "1", True, False)
Next
End If
'Debug.Print c.name & " " & c.Tag & " " & b(0) & " " & b(1) & " " & b(2)
AddItem c, b(0), b(1), b(2)
Erase tmp()
Erase b() 'they all default to false again
Next

End Sub

Sub AddItem(c As Control, Optional anchorTopLeft As Boolean = True, Optional


fixedHeight As Boolean, Optional fixedWidth As Boolean)

15 anchor/Form1.frm
@@ -18,12 +18,12 @@ Begin VB.Form Form1
Width = 1005
End
Begin VB.CommandButton Command1
Caption = "Command1"
Caption = "Form2 Example"
Height = 375
Left = 5670
Left = 5310
TabIndex = 3
Top = 5670
Width = 1095
Width = 1455
End
Begin VB.TextBox Text2
Height = 3075
@@ -45,6 +45,7 @@ Begin VB.Form Form1
Height = 5910
Left = 45
TabIndex = 0
Tag = "tes tag"
Top = 135
Width = 2310
End
@@ -56,8 +57,16 @@ Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Dim anchor As New CAnchor

Private Sub Command1_Click()


Form2.Visible = True
End Sub

Private Sub Form_Load()

'you can actually reduce this down to one line of code


'per form, see the form2 example. .tag attributes are saved in the frm file
'note this is very subtle though, unfamiliar devs would have no idea wtf was
going on

Set anchor.owner = Me
anchor.AddItem List1, True, , True
anchor.AddItem Text2
81 anchor/Form2.frm
@@ -0,0 +1,81 @@
VERSION 5.00
Begin VB.Form Form2
Caption = "Form2"
ClientHeight = 8085
ClientLeft = 60
ClientTop = 345
ClientWidth = 8145
LinkTopic = "Form2"
ScaleHeight = 8085
ScaleWidth = 8145
StartUpPosition = 3 'Windows Default
Begin VB.TextBox Text3
Height = 1995
Left = 2610
TabIndex = 5
Text = "This one has no tag"
Top = 2790
Width = 5370
End
Begin VB.CommandButton Command2
Caption = "Command2"
Height = 375
Left = 6885
TabIndex = 4
Tag = "0,1,1"
Top = 7380
Width = 1005
End
Begin VB.CommandButton Command1
Caption = "Command1"
Height = 375
Left = 5670
TabIndex = 3
Tag = "0,1,1"
Top = 7380
Width = 1095
End
Begin VB.TextBox Text2
Height = 2445
Left = 2610
TabIndex = 2
Tag = "1,1"
Text = "this one has only first 2 attributes set in tag"
Top = 180
Width = 5370
End
Begin VB.TextBox Text1
Height = 2220
Left = 2610
TabIndex = 1
Tag = "0,1,0"
Text = "Text1"
Top = 4995
Width = 5370
End
Begin VB.ListBox List1
Height = 7080
Left = 45
TabIndex = 0
Tag = "1,0,1"
Top = 135
Width = 2310
End
End
Attribute VB_Name = "Form2"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Dim anchor As New CAnchor

Private Sub Form_Load()

'this example uses resize attributes saved in


'each form elements .tag, partial or no attributes are ok
'as long as you understand the default behaviors

anchor.LoadSettingsFromTags Me

End Sub

3 anchor/Project1.vbp
@@ -1,8 +1,9 @@
Type=Exe
Form=Form1.frm
Reference=*\G{00020430-0000-0000-C000-
000000000046}#2.0#0#..\..\..\..\WINDOWS\system32\stdole2.tlb#OLE Automation
Reference=*\G{00020430-0000-0000-C000-
000000000046}#2.0#0#C:\WINDOWS\system32\stdole2.tlb#OLE Automation
Class=CAnchor; CAnchor.cls
Class=CAnchorElement; CAnchorElement.cls
Form=Form2.frm
IconForm="Form1"
Startup="Form1"
ExeName32="Project1.exe"
5 anchor/Project1.vbw
@@ -1,3 +1,4 @@
Form1 = 44, 44, 688, 623, , 22, 22, 666, 601, C
CAnchor = 9, 5, 896, 860, C
Form1 = 44, 44, 688, 623, Z, 22, 22, 666, 601, C
CAnchor = 9, 5, 896, 860,
CAnchorElement = 88, 88, 732, 667, C
Form2 = 66, 66, 710, 645, , 44, 44, 688, 623, C

You might also like