patternMinor
Honey I shrunk the view
Viewed 0 times
shrunktheviewhoney
Problem
Going down the rabbit hole, I wanted to be able to change the size of a form, and not have to worry about the layout of the controls.
vba doesn't support user-resizeable forms, but forms still have a
I'm leveraging this event handler to set up my automatic layout, like this:
That code is reviewable as part of this post, and it's the client code of a class I'd like reviewed here:
ControlLayout class module
There's no magic, I haven't implemented a wpf-like autom
vba doesn't support user-resizeable forms, but forms still have a
Resize event that fires when a form's size is programmatically changed.I'm leveraging this event handler to set up my automatic layout, like this:
Private Sub BindControlLayouts()
Dim backgroundImageLayout As New ControlLayout
backgroundImageLayout.Bind Me, BackgroundImage, AnchorAll
Dim closeButtonLayout As New ControlLayout
closeButtonLayout.Bind Me, CloseButton, BottomAnchor + RightAnchor
Dim itemsListLayout As New ControlLayout
itemsListLayout.Bind Me, ItemsList, AnchorAll
Dim addButtonLayout As New ControlLayout
addButtonLayout.Bind Me, AddButton, RightAnchor
Dim editButtonLayout As New ControlLayout
editButtonLayout.Bind Me, EditButton, RightAnchor
Dim showDetailsButtonLayout As New ControlLayout
showDetailsButtonLayout.Bind Me, ShowDetailsButton, RightAnchor
Dim deleteButtonLayout As New ControlLayout
deleteButtonLayout.Bind Me, DeleteButton, RightAnchor
layoutBindings.Add closeButtonLayout, _
backgroundImageLayout, _
itemsListLayout, _
addButtonLayout, _
editButtonLayout, _
showDetailsButtonLayout, _
deleteButtonLayout
End Sub
Private Sub UserForm_Resize()
Application.ScreenUpdating = False
If Me.Width < minWidth Then Me.Width = minWidth
If Me.Height < minHeight Then Me.Height = minHeight
Dim layout As ControlLayout
For Each layout In layoutBindings
layout.Resize Me
Next
Application.ScreenUpdating = True
End SubThat code is reviewable as part of this post, and it's the client code of a class I'd like reviewed here:
ControlLayout class module
There's no magic, I haven't implemented a wpf-like autom
Solution
I really like this method of declaring a bitwise enum.
I find it much preferable to the way I usually see it done.
But you don't need all of this.
The condition can be written simply as
I would recommend leaving a small comment alerting the unwary maintainer that it is a bitwise
Public Enum AnchorEdges
LeftAnchor = 2 ^ 0
TopAnchor = 2 ^ 1
RightAnchor = 2 ^ 2
BottomAnchor = 2 ^ 3
AnchorAll = LeftAnchor + TopAnchor + RightAnchor + BottomAnchor
End EnumI find it much preferable to the way I usually see it done.
Public Enum BitWiseEnum
SomeValue = 1
SomeValue = 2
SomeValue = 4
SomeValue = 8
End EnumBut you don't need all of this.
If (anchor And BottomAnchor) = BottomAnchor ThenThe condition can be written simply as
If (anchor And BottomAnchor) ThenI would recommend leaving a small comment alerting the unwary maintainer that it is a bitwise
And not a logical And though. I don't imagine too many VBA Maintainers will understand this code at a glance.Code Snippets
Public Enum AnchorEdges
LeftAnchor = 2 ^ 0
TopAnchor = 2 ^ 1
RightAnchor = 2 ^ 2
BottomAnchor = 2 ^ 3
AnchorAll = LeftAnchor + TopAnchor + RightAnchor + BottomAnchor
End EnumPublic Enum BitWiseEnum
SomeValue = 1
SomeValue = 2
SomeValue = 4
SomeValue = 8
End EnumIf (anchor And BottomAnchor) = BottomAnchor ThenIf (anchor And BottomAnchor) ThenContext
StackExchange Code Review Q#58349, answer score: 6
Revisions (0)
No revisions yet.