Results 1 to 6 of 6

Thread: VB/VBA: Set TextBox and Label text to Vertically Align Center

Threaded View

  1. #3
    New Member
    Join Date
    Dec 2009
    Posts
    1

    Re: VB/VBA: Set TextBox and Label text to Vertically Align Center

    That was such a useful piece of code, the easiest to find on google. I tweaked it to allow multiple lines of code... cheesy, probably, but it worked for me. Thanks for the super code, and I hope someone finds my tweak useful/functional.
    vb Code:
    1. Public Sub VerticalAlignCenter(ByRef ctl As Control)
    2. On Error GoTo ErrorCode
    3.     Dim MinimumMargin As Integer
    4.     Dim BorderWidth As Integer
    5.     Dim TwipsPerPoint
    6.     TwipsPerPoint = 20
    7.     If Not ((TypeOf ctl Is TextBox) Or (TypeOf ctl Is Label)) Then Exit Sub
    8.    'Figure out how many lines it is
    9.    Dim LenOfText, WidOfBox, NumberOfLines, HtOfText
    10.    If TypeOf ctl Is TextBox Then
    11.     LenOfText = ctl.Text
    12.     Else:
    13.     LenOfText = ctl.Caption
    14.     End If
    15.     'how wide is this puppy?
    16.     WidOfBox = ctl.Width
    17.     LenOfText = (Len(LenOfText) * TwipsPerPoint * ctl.FontSize) / 2
    18.     NumberOfLines = Int(LenOfText / WidOfBox) + 1
    19.     HtOfText = NumberOfLines * TwipsPerPoint * ctl.FontSize
    20.    
    21.    
    22.     MinimumMargin = 1 * TwipsPerPoint
    23.     BorderWidth = (ctl.BorderWidth * TwipsPerPoint) / 2
    24.    
    25.     ctl.TopMargin = ((ctl.Height - HtOfText) / 2) - MinimumMargin - BorderWidth    
    26.    
    27. ErrorCode:
    28.     Exit Sub
    29. End Sub
    Last edited by Hack; Sep 2nd, 2011 at 09:04 AM. Reason: Added Highlight Tags

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width