HiveBrain v1.2.0
Get Started
← Back to all entries
patternMinor

Honey I shrunk the view

Submitted by: @import:stackexchange-codereview··
0
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 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 Sub


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

Solution

I really like this method of declaring a bitwise enum.

Public Enum AnchorEdges
    LeftAnchor = 2 ^ 0
    TopAnchor = 2 ^ 1
    RightAnchor = 2 ^ 2
    BottomAnchor = 2 ^ 3
    AnchorAll = LeftAnchor + TopAnchor + RightAnchor + BottomAnchor
End Enum


I find it much preferable to the way I usually see it done.

Public Enum BitWiseEnum
    SomeValue = 1
    SomeValue = 2
    SomeValue = 4
    SomeValue = 8
 End Enum


But you don't need all of this.

If (anchor And BottomAnchor) = BottomAnchor Then


The condition can be written simply as

If (anchor And BottomAnchor) Then


I 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 Enum
Public Enum BitWiseEnum
    SomeValue = 1
    SomeValue = 2
    SomeValue = 4
    SomeValue = 8
 End Enum
If (anchor And BottomAnchor) = BottomAnchor Then
If (anchor And BottomAnchor) Then

Context

StackExchange Code Review Q#58349, answer score: 6

Revisions (0)

No revisions yet.