VBcoders Guest



Don't have an account yet? Register
 


Forgot Password?



Anchor Class

by Ferry Timmers (1 Submission)
Category: VB function enhancement
Compatability: VB Script
Difficulty: Intermediate
Date Added: Wed 3rd February 2021
Rating: (5 Votes)

Anchors, makes controls size along with the form.
I wanted to use anchors in VB and make this class in order to save some time. I hope you'll find it usefull.

Assumes
You need to know how to use classes

Rate Anchor Class

'-----------------------------------------------------
'         Costum Anchor Class
'            for VB
'        by Ferry "FTPlus" Timers
'-----------------------------------------------------
'
' Usage: (in 4 steps)
'
' 1. Declare as a new class
'
' example: Dim ancTextbox1 as new Anchor
'
' 2. Initalize the anchor and hook the control to the class (init before movement)
'  (it remembers the margins)
'
' example: anc.Textbox1.Init Textbox1
'
' 3. Set the anchor points (Left, Top, Right, Bottom)
'
' example: ancTextbox1.SetAnchors True, False, True, True
'
' 4. And Finaly lets justify the sizes (best to place in Form_Resize)
'
' example: ancTextbox1.Update
'
'-----------------------------------------------------
'           Have Fun
'-----------------------------------------------------
Private Type tAnchor
 Left As Boolean
 Top As Boolean
 Right As Boolean
 Bottom As Boolean
End Type
Public Container As Object
Private Anchors As tAnchor
Private marginLeft As Integer
Private marginTop As Integer
Private marginRight As Integer
Private marginBottom As Integer
Public Sub Init(ByRef Obj As Object)
Set Container = Obj
With Container
 marginLeft = .Left
 marginTop = .Top
 marginRight = .Container.Width - .Left - .Width
 marginBottom = .Container.Height - .Top - .Height
End With
End Sub
Public Sub SetAnchors(Left As Boolean, Top As Boolean, Right As Boolean, Bottom As Boolean)
With Anchors
 .Left = Left
 .Top = Top
 .Right = Right
 .Bottom = Bottom
End With
End Sub
Public Sub Update()
Dim I As Integer
If Anchors.Left Then
 Container.Left = marginLeft
 If Anchors.Right Then
  I = Container.Container.Width - marginLeft - marginRight
  If I > 0 Then Container.Width = I
 End If
ElseIf Anchors.Right Then
 I = Container.Container.Width - Container.Width - marginRight
 If I > 0 Then Container.Left = I
End If
If Anchors.Top Then
 Container.Top = marginTop
 If Anchors.Bottom Then
  I = Container.Container.Height - marginTop - marginBottom
  If I > 0 Then Container.Height = I
 End If
ElseIf Anchors.Bottom Then
 I = Container.Container.Height - Container.Height - marginBottom
 If I > 0 Then Container.Top = I
End If
End Sub

Download this snippet    Add to My Saved Code

Anchor Class Comments

No comments have been posted about Anchor Class. Why not be the first to post a comment about Anchor Class.

Post your comment

Subject:
Message:
0/1000 characters