Home  |  About  | Last |  Submit  |  Contact
AllQuests.com



Previous Question:  VBA - Working with Scroll Bars - The most simplest way  CodeBank OtherNext Question:  Reg for help with overclocking projects  General Hardware
Question VB/VBA: Set TextBox and Label text to Vertically Align Center ( VBForums CodeBank Other )
Updated: 2008-04-19 17:36:54 (1)
VB/VBA: Set TextBox and Label text to Vertically Align Center

Hi all, working in Access 2003, I was annoyed that I couldn't get my labels to align vertically unless I manually played with the .TopMargin property of each control. I whipped up this function that aligns the text vertically so I wouldn't need to do it manually anymore. Hopefully it can be of use to some of you

Code:
'Sets the vertical alignment of a label or textbox to "Center" 'NOTE: 'MinimumMargin: the value of the smallest margin Access allows in twips. If you set the .TopMargin 'to "0", Access will actually place a gap equal to 1 point (or 20 twips) rather than actually 'placing the text at the very top border of the control. FURTHER NOTE: Access sucks. ' 'BorderWidth: half the value of .BorderWidth in twips. As you increase the border (Hairline, 1pt, 2pt, etc..) 'Access expands the border equally from the center. We're only concerned with the "inner" section of the border. ' 'TwipsPerPoint: global constant set to 20 Public Sub VerticalAlignCenter(ByRef ctl As Control) On Error GoTo ErrorCode Dim MinimumMargin As Integer Dim BorderWidth As Integer If Not ((TypeOf ctl Is TextBox) Or (TypeOf ctl Is Label)) Then Exit Sub MinimumMargin = 1 * TwipsPerPoint BorderWidth = (ctl.BorderWidth * TwipsPerPoint) / 2 ctl.TopMargin = ((ctl.Height - (ctl.FontSize * TwipsPerPoint)) / 2) - MinimumMargin - BorderWidth ErrorCode: Exit Sub End Sub

Chop it up, modify it, use it however you want. If you improve it in any way, please post your changes so that I can learn..



Update: My code doesn't work for controls with multiple lines of text.. It sets the TopMargin as if the top line of text is the only line present in the control. Does anyone have any creative workarounds for this glitch?

Answers: VB/VBA: Set TextBox and Label text to Vertically Align Center ( VBForums CodeBank Other )
VB/VBA: Set TextBox and Label text to Vertically Align Center

Thread Moved

Thanks for the submission


You might want to note that it will require a MSForms Textbox or Label control if being used outside of VBA.

RobDog888

Previous Question:  VBA - Working with Scroll Bars - The most simplest way  VBForums  CodeBank OtherNext Question:  Reg for help with overclocking projects  2CPU.com Discussion Forums  General Hardware

- Source: VB/VBA: Set TextBox and Label text to Vertically Align Center VBForums CodeBank Other
- Previous Question: VBA - Working with Scroll Bars - The most simplest way VBForums CodeBank Other
- Next Question: Reg for help with overclocking projects 2CPU.com Discussion Forums General Hardware