Results 1 to 27 of 27

Thread: [RESOLVED] How do I vertically align the text in a textbox?

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Sep 2024
    Posts
    19

    Resolved [RESOLVED] How do I vertically align the text in a textbox?

    I've been skimming through M$'s Windows Interface Guidelines and it showed an example of the "correct" textbox height; however, when replicating this in the VB IDE, it aligns the text at the top vertically of the textbox instead of the center and additionally doesn't have the same padding on the left. Anyone know any fixes or methods to attain the results in Picture A? The padding isn't especially important, but I'd really like to fix the alignment of the text.

    Name:  VBForums.png
Views: 225
Size:  12.7 KB

    Name:  text.png
Views: 209
Size:  9.2 KB

    Thanks,
    John
    Last edited by JohnCl; Sep 7th, 2024 at 04:39 PM.

  2. #2
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    10,526

    Re: How do I vertically align the text in a textbox?

    If I was that insistent upon getting vertical alignment, I'd probably use the RTB, and then figure out the RTF codes to achieve it.

    Without a great deal of work (and maybe not at all), I don't believe you're going to get vertical alignment in a standard textbox.



    I suppose another alternative is to make a UserControl, where the TextBox was inside a PictureBox with the same backcolor and had the border you wanted (no borders on the TextBox). Done that way, you could move the TextBox down vertically to achieve it. You'd be making some novel use of the TextHeight method to get it done though, but it'd certainly be possible.
    Last edited by Elroy; Sep 7th, 2024 at 05:09 PM.
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

  3. #3
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,503

    Re: How do I vertically align the text in a textbox?

    I don't understand. I simply added a textbox and then typed in it at runtime....sure looks vertically aligned to me. ????
    Attached Images Attached Images  
    Sam I am (as well as Confused at times).

  4. #4
    Addicted Member
    Join Date
    Jul 2021
    Posts
    252

    Re: How do I vertically align the text in a textbox?

    Starting from Windows Vista, there is a EM_SETRECT message to manually set the padding for the EDIT control (TextBox).
    The only condition - it must be a multi-line TextBox.

    So, you can use the following function to align the text vertically. You can block entering new line in the KeyPress event.

    Code:
    Private Const EM_SETRECT As Long = &HB3&
    
    Private Declare Function GetClientRect Lib "user32" (ByVal hwnd As Long, ByVal lpRect As Long) As Long
    Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    
    Private Sub Text1_KeyPress(KeyAscii As Integer)
    If KeyAscii = 13 Then KeyAscii = 0
    End Sub
    
    Sub SetVAlign(TB As TextBox)
    Dim a(4) As Long
    Dim LineHeight As Long
    Set Font = TB.Font
    LineHeight = ScaleY(TextHeight("X"), ScaleMode, vbPixels)
    GetClientRect TB.hwnd, VarPtr(a(0))
    a(1) = (a(3) - LineHeight) \ 2
    a(3) = a(3) - a(1)
    SendMessage TB.hwnd, EM_SETRECT, 0, VarPtr(a(0))
    End Sub

  5. #5
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    10,526

    Re: How do I vertically align the text in a textbox?

    Quote Originally Posted by SamOscarBrown View Post
    I don't understand. I simply added a textbox and then typed in it at runtime....sure looks vertically aligned to me. ????
    Sam. You know if you were to vertically stretch it, but not change the font size, that it wouldn't be vertically aligned.
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

  6. #6
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,503

    Re: How do I vertically align the text in a textbox?

    I know, Elroy, but why do that? Just create it the 'right' size in the first place. Of course it would be dependent upon fontsize, but, jeez, just resize the box. No??? After all, it is not a user thing, but a developer one. IF a user has an option to change fontsize, then have code change the size of the textbox accordingly (in relation to fontsize).
    Sam I am (as well as Confused at times).

  7. #7
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,503

    Re: How do I vertically align the text in a textbox?

    Been playing around with this a bit....So, I created three textboxes, each of Font Ariel. First Font is 12PT, second Font is 24Pt and third Font is 36 Point. (270, 540, 825 twips, respectively)

    Using the mouse, I initially made each textbox height much larger than the font (stretched the textboxes vertically). Then, for each textbox, I contracted it to the minimum it would allow me using the mouse. At that point, the text appears vertically centered. The minimum I could set the height of each textbox (in twips) was 345, 615, 900.

    There must be some sort of correlation to determine how much 'padding' VB6 adds to the top of the textbox to the top of the text, which should then be equal to the 'padding' at the bottom of the text to the bottom of the textbox...but I could not (yet) determine that correlation. It is probably different for each font, possibly.

    My code below (I had some commented out lines in my testing) resulted in outputs of:

    Height of Arial (12pt): 270 twips
    Height of Ariel (24pt): 540 twips
    Height of Ariel (36pt): 825 twips
    Text1.Height: 345
    Text2.Height: 615
    Text3.Height: 900
    Code:
    Private Sub Form_Load()
     ' Variables to hold font heights
        Dim heightArial12 As Integer
        Dim heightArial24 As Integer
        Dim heightArial36 As Integer
        
        Dim heightTimesNewRoman As Integer
        Dim heightCourierNew As Integer
    
    
        ' Set font to Arial and determine text height
        Me.Font.Name = "Arial"
        Me.Font.Size = 12
        heightArial12 = Me.TextHeight("Sample Text")
    
    
        Me.Font.Name = "Arial"
        Me.Font.Size = 24
        heightArial24 = Me.TextHeight("Sample Text")
        
        Me.Font.Name = "Arial"
        Me.Font.Size = 36
        heightArial36 = Me.TextHeight("Sample Text")
        
    '    ' Set font to Times New Roman and determine text height
    '    Me.Font.Name = "Times New Roman"
    '    Me.Font.Size = 12
    '    heightTimesNewRoman = Me.TextHeight("Sample Text")
    '
    '    ' Set font to Courier New and determine text height
    '    Me.Font.Name = "Courier New"
    '    Me.Font.Size = 12
    '    heightCourierNew = Me.TextHeight("Sample Text")
    
    
        ' Display the results in a message box
    '    Debug.Print "Height of Arial (12pt): " & heightArial & " twips" & vbCrLf & _
    '           "Height of Times New Roman (12pt): " & heightTimesNewRoman & " twips" & vbCrLf & _
    '           "Height of Courier New (12pt): " & heightCourierNew & " twips"
            
            Debug.Print "Height of Arial (12pt): " & heightArial12 & " twips" & vbCrLf & _
               "Height of Ariel (24pt): " & heightArial24 & " twips" & vbCrLf & _
               "Height of Ariel (36pt): " & heightArial36 & " twips"
            Debug.Print "Text1.Height: " & Text1.Height
            Debug.Print "Text2.Height: " & Text2.Height
            Debug.Print "Text3.Height: " & Text3.Height
    End Sub
    But, bottom line, if one makes the textbox height as small as possible (using the mouse), the text should be centered.

    This is what my form looked like in .exe:

    Name:  textboxesfont.jpg
Views: 176
Size:  10.6 KB
    Sam I am (as well as Confused at times).

  8. #8
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,503

    Re: How do I vertically align the text in a textbox?

    Oh, wait a minute...I see that 75 is the difference between textbox height and font height. So, 1/2 of that is padding on top, and 1/2 on the bottom.

    I can now see that in order to vertically center text, set the textbox height to fontsize (in twips) plus 75 twips.


    EDIT: On second thought, that may be relational to screen resolution. And may be different for different fonts. Gonna check all that later today....
    Last edited by SamOscarBrown; Sep 8th, 2024 at 08:56 AM.
    Sam I am (as well as Confused at times).

  9. #9
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,503

    Re: How do I vertically align the text in a textbox?

    I changed the fonts to Calibri....'resized' each textbox with the mouse (as described above). Difference between font Height and textbox height is again, 75.

    Resolution: 1920 X 1080

    Changed screen resolution...same calculations (difference is 75)

    So, I would say if you want to dynamically center the text vertically, get the height of the font (shown in code in post #7), and set the textbox height to that value plus 75.

    What do you all think?

    EDIT:

    for example: I created a textbox with font MS Sans Serif--left the default text as "Text4". I made the textbox.height considerably larger so that the text did not appear to be centered.

    Then I ran this code and it made the textbox the correct size to appear that the text was vertically centered:

    Code:
        Dim heightMSSansSerif As Integer
        
        Text4.Font.Name = "MS Sans Serif"
        Text4.Font.Size = 8
         heightMSSansSerif = Me.TextHeight("Text4")
        Text4.height = heightMSSansSerif + 75
    Last edited by SamOscarBrown; Sep 8th, 2024 at 01:03 PM.
    Sam I am (as well as Confused at times).

  10. #10
    Fanatic Member
    Join Date
    Jul 2007
    Location
    Essex, UK.
    Posts
    579

    Re: How do I vertically align the text in a textbox?

    Maybe first post just shows the difference between 'flat' and '3d'

  11. #11

    Thread Starter
    Junior Member
    Join Date
    Sep 2024
    Posts
    19

    Re: How do I vertically align the text in a textbox?

    Quote Originally Posted by Steve Grant View Post
    Maybe first post just shows the difference between 'flat' and '3d'
    Sorry about that, bad example. The box only looks flat due to a WinXP manifest, the text alignment is the same regardless.

  12. #12
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,503

    Re: How do I vertically align the text in a textbox?

    John...did you try what I posted?
    Sam I am (as well as Confused at times).

  13. #13

    Thread Starter
    Junior Member
    Join Date
    Sep 2024
    Posts
    19

    Re: How do I vertically align the text in a textbox?

    Quote Originally Posted by SamsOscarBrown View Post
    John...did you try what I posted?
    It works great for resizing a textbox to fit the font size, but what I'm looking for is something that allows textboxes to be the same height as in official MS applications (taller than the default textbox height in VB) while not having the text at the top and instead at the vertical middle.

    Thank you for your contribution anyway, as it encouraged me to play around with resizing form elements.
    Last edited by JohnCl; Sep 9th, 2024 at 04:02 PM.

  14. #14
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,503

    Re: How do I vertically align the text in a textbox?

    Ah, I see. You want a tall(er) text box and have the text vertically centered.

    Why not experiment with picture boxes—-bet you can do that there!!!!!
    Last edited by SamOscarBrown; Sep 10th, 2024 at 07:30 AM.
    Sam I am (as well as Confused at times).

  15. #15
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    10,526

    Re: How do I vertically align the text in a textbox?

    Again, if I wanted this, I'd probably knock out a custom UC to get this done. PictureBox on your UC, and just move around a borderless TextBox to vertically center it.

    You might actually get away without a PictureBox and just use the UC's borders to get whatever border effects you want, but I haven't played around with that. I'd consider knocking this out, but I'm travelling, and just on my Surface tablet. If I find some time, I might knock it out anyway.
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

  16. #16
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,503

    Re: How do I vertically align the text in a textbox?

    I played around with the Picturebox....used AI to create the code...didn't quite work...text appeared slightly closer to the bottom than the top. I kept tweaking my AI input, and it continued to provide 'suggestions'. Ultimately, those suggestions resulted in me modifying the code presented with 'adjustment values' that actually made it look centered. Those would be different values for different fonts. So I gave up.

    Here is the ORIGINAL AI code...maybe you can check and find out why it is not accurate (very obvious for smaller (12 and below) text sizes).

    (Picturebox AutoRedraw is set to True)

    Code:
    Option Explicit
    
    
    Private Sub Form_Load()
        ' Text to display in the PictureBox
        Dim myText As String
        myText = "This is centered vertically and left-aligned text."
    
    
        ' Set PictureBox Height to twice the Text Height
        Dim textHeight As Single
        textHeight = Picture1.textHeight(myText)
        Picture1.Height = 2 * textHeight
    
    
        ' Clear the PictureBox and set the font and other properties
        Picture1.Cls
        Picture1.Font = Me.Font ' Use the form's font for consistency
        
        ' Get the vertical position to center the text in the PictureBox
        Dim verticalPosition As Single
        verticalPosition = (Picture1.ScaleHeight - textHeight) / 2
    
    
        ' Draw the text left-aligned and centered vertically
        Picture1.CurrentX = 0 ' Left-align the text
        Picture1.CurrentY = verticalPosition ' Center it vertically
        Picture1.Print myText
    End Sub
    Sam I am (as well as Confused at times).

  17. #17
    Addicted Member
    Join Date
    Jul 2021
    Posts
    252

    Re: How do I vertically align the text in a textbox?

    One provides a good solution and just being ignored. Oh well...

  18. #18
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,503

    Re: How do I vertically align the text in a textbox?

    Quote Originally Posted by Dry Bone View Post
    One provides a good solution and just being ignored. Oh well...
    How do you call that sub? What is passed into it?

    EDIT: That's pretty cool/short code!
    Last edited by SamOscarBrown; Sep 10th, 2024 at 02:59 PM.
    Sam I am (as well as Confused at times).

  19. #19
    Addicted Member
    Join Date
    Jul 2021
    Posts
    252

    Re: How do I vertically align the text in a textbox?

    Just:
    Code:
    SetVAlign Text1
    This should make the text on Text1 vertically aligned, given that it is a multi-line textbox having one line of text

  20. #20
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    10,526

    Re: How do I vertically align the text in a textbox?

    Ok, I knocked out a UC on my tablet (no mouse, only touch-screen). I think I got all the essentials covered.

    I did make a couple of assumptions though:
    1. Only single-line TextBoxes.
    2. We won't be using any Scrollbars.

    And, I tested some, but not thoroughly.

    I covered all the most common events and properties, but there are a few I didn't pass through.

    One I can think of is the hWnd to the TextBox. That might be useful in certain instances. Also, I didn't mess with any of the OLE stuff.

    Here's a screenshot:

    Name:  Image1.jpg
Views: 122
Size:  14.4 KB

    Sample project is attached, but here's the code in the custom UC (which is really the only code):

    Code:
    Option Explicit
    '
    Public Enum BS_Special
        txtBsOff = 0&
        txtBsOn = 1&
    End Enum
    '
    Public Enum Appear_Special
        txtAppearFlat = 0&
        txtAppear3D = 1&
    End Enum
    '
    ' Our custom UC events:
    Event Change()
    Event Click()
    Event DblClick()
    Event KeyDown(KeyCode As Integer, Shift As Integer)
    Event KeyPress(KeyAscii As Integer)
    Event KeyUp(KeyCode As Integer, Shift As Integer)
    Event MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    Event MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
    Event MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
    Event Validate(Cancel As Boolean)
    '
    
    
    ' ***************************************************
    '
    ' The properties that affect vertical centering.
    '
    ' ***************************************************
    
    Public Property Set Font(f As StdFont)
        Set txt.Font = f
        AdjustTextBoxPosition
        PropertyChanged
    End Property
    
    Public Property Get Font() As StdFont
        Set Font = txt.Font
    End Property
    
    Public Property Let BorderStyle(bs As BS_Special)
        UserControl.BorderStyle = bs
        AdjustTextBoxPosition
        PropertyChanged
    End Property
    
    Public Property Get BorderStyle() As BS_Special
        BorderStyle = UserControl.BorderStyle
    End Property
    
    Public Property Let Appearance(Appear As Appear_Special)
        UserControl.Appearance = Appear
        AdjustTextBoxPosition
        PropertyChanged
    End Property
    
    Public Property Get Appearance() As Appear_Special
        Appearance = UserControl.Appearance
    End Property
    
    ' ***************************************************
    '
    ' Other properties we'll deal with.
    '
    ' ***************************************************
    
    Public Property Let Alignment(align As AlignmentConstants)
        txt.Alignment = align
        PropertyChanged
    End Property
    
    Public Property Get Alignment() As AlignmentConstants
        Alignment = txt.Alignment
    End Property
    
    Public Property Let BackColor(bc As OLE_COLOR)
        txt.BackColor = bc
        UserControl.BackColor = bc
        PropertyChanged
    End Property
    
    Public Property Get BackColor() As OLE_COLOR
        BackColor = txt.BackColor
    End Property
    
    Public Property Let ForeColor(fc As OLE_COLOR)
        txt.ForeColor = fc
        PropertyChanged
    End Property
    
    Public Property Get ForeColor() As OLE_COLOR
        ForeColor = txt.ForeColor
    End Property
    
    Public Property Let Text(s As String)
        txt.Text = s
        PropertyChanged
    End Property
    
    Public Property Get Text() As String
        Text = txt.Text
    End Property
    
    Public Property Let MaxLength(i As Integer)
        If i < 0& Then i = 0&
        txt.MaxLength = i
        PropertyChanged
    End Property
    
    Public Property Get MaxLength() As Integer
        MaxLength = txt.MaxLength
    End Property
    
    Public Property Let Locked(b As Boolean)
        txt.Locked = b
        PropertyChanged
    End Property
    
    Public Property Get Locked() As Boolean
        Locked = txt.Locked
    End Property
    
    ' ***************************************************
    '
    ' The events we'll raise in our custom UC.
    '
    ' ***************************************************
    
    Private Sub txt_Change()
        RaiseEvent Change
    End Sub
    
    Private Sub txt_Click()
        RaiseEvent Click
    End Sub
    
    Private Sub txt_DblClick()
        RaiseEvent DblClick
    End Sub
    
    Private Sub txt_KeyDown(KeyCode As Integer, Shift As Integer)
        RaiseEvent KeyDown(KeyCode, Shift)
    End Sub
    
    Private Sub txt_KeyPress(KeyAscii As Integer)
        RaiseEvent KeyPress(KeyAscii)
    End Sub
    
    Private Sub txt_KeyUp(KeyCode As Integer, Shift As Integer)
        RaiseEvent KeyUp(KeyCode, Shift)
    End Sub
    
    Private Sub txt_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
        RaiseEvent MouseDown(Button, Shift, X, Y)
    End Sub
    
    Private Sub txt_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
        RaiseEvent MouseMove(Button, Shift, X, Y)
    End Sub
    
    Private Sub txt_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
        RaiseEvent MouseUp(Button, Shift, X, Y)
    End Sub
    
    Private Sub txt_Validate(Cancel As Boolean)
        RaiseEvent Validate(Cancel)
    End Sub
    
    ' ***************************************************
    '
    ' Our UserControl events.
    '
    ' ***************************************************
    
    Private Sub UserControl_ReadProperties(PropBag As PropertyBag)
        '
        ' Ones that affect vertical centering.
        Set txt.Font = PropBag.ReadProperty("Font", txt.Font)
        txt.BorderStyle = PropBag.ReadProperty("BorderStyle", txt.BorderStyle)
        txt.Appearance = PropBag.ReadProperty("Appearance", txt.Appearance)
        '
        ' The rest of them we handle.
        txt.Alignment = PropBag.ReadProperty("Alignment", txt.Alignment)
        txt.BackColor = PropBag.ReadProperty("BackColor", txt.BackColor)
        txt.ForeColor = PropBag.ReadProperty("ForeColor", txt.ForeColor)
        txt.Text = PropBag.ReadProperty("Text", txt.Text)
        txt.MaxLength = PropBag.ReadProperty("MaxLength", txt.MaxLength)
        txt.Locked = PropBag.ReadProperty("Locked", txt.Locked)
    End Sub
    
    Private Sub UserControl_WriteProperties(PropBag As PropertyBag)
        '
        ' Ones that affect vertical centering.
        PropBag.WriteProperty "Font", txt.Font
        PropBag.WriteProperty "BorderStyle", txt.BorderStyle
        PropBag.WriteProperty "Appearance", txt.Appearance
        '
        ' The rest of them we handle.
        PropBag.WriteProperty "Alignment", txt.Alignment
        PropBag.WriteProperty "BackColor", txt.BackColor
        PropBag.WriteProperty "ForeColor", txt.ForeColor
        PropBag.WriteProperty "Text", txt.Text
        PropBag.WriteProperty "MaxLength", txt.MaxLength
        PropBag.WriteProperty "Locked", txt.Locked
    End Sub
    
    Private Sub UserControl_Resize()
        AdjustTextBoxPosition
    End Sub
    
    ' ***************************************************
    '
    ' Private helper procedures.
    '
    ' ***************************************************
    
    Private Sub AdjustTextBoxPosition()
        txt.Left = 0!                           ' This one should be straightforward.
        txt.Width = UserControl.ScaleWidth      ' As well as this one.
        txt.Height = UserControl.ScaleHeight    ' And this one as well.
        '
        ' The Top is the tricky one for vertical centering.
        ' We shouldn't have to worry about borders, as that's handled by ScaleHeight.
        Set UserControl.Font = txt.Font
        Dim fTextHeight As Single
        fTextHeight = UserControl.TextHeight("asdf")
        txt.Top = (UserControl.ScaleHeight - fTextHeight) / 2!
        '
        ' And this seems to be needed to make sure they stay the same.
        UserControl.BackColor = txt.BackColor
    End Sub
    Attached Files Attached Files
    Last edited by Elroy; Sep 11th, 2024 at 12:45 PM.
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

  21. #21
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    10,526

    Re: How do I vertically align the text in a textbox?

    Here's another (improved) version.

    I added three new properties:

    1. AlignVert, so you can specify Top, Center, or Bottom.
    2. LeftMargin, so you can get the first character off the left edge (in twips).
    3. hWnd_txt, runtime only, just seemed like it might occasionally be useful.


    New version attached.

    Here's the procedure that does the magic:

    Code:
    Private Sub AdjustTextBoxPosition()
        txt.Left = mfLeftMargin
        txt.Width = UserControl.ScaleWidth - mfLeftMargin
        txt.Height = UserControl.ScaleHeight
        '
        ' The Top is the tricky one for vertical centering.
        ' We shouldn't have to worry about borders, as that's handled by ScaleHeight.
        If miVertAlign = txtVertTop Then
            txt.Top = 0!
        Else
            Set UserControl.Font = txt.Font
            Dim fTextHeight As Single
            fTextHeight = UserControl.TextHeight("asdf")
            If miVertAlign = txtVertCenter Then
                txt.Top = (UserControl.ScaleHeight - fTextHeight) / 2!
            Else ' txtVertBottom.
                txt.Top = UserControl.ScaleHeight - fTextHeight
            End If
        End If
        '
        ' And this seems to be needed to make sure they stay the same.
        UserControl.BackColor = txt.BackColor
    End Sub
    Attached Files Attached Files
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

  22. #22
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,503

    Re: How do I vertically align the text in a textbox?

    Well...very good, Elroy (as usual)!
    Sam I am (as well as Confused at times).

  23. #23
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    10,526

    Re: How do I vertically align the text in a textbox?

    Quote Originally Posted by SamOscarBrown View Post
    Well...very good, Elroy (as usual)!
    Yeah, custom UCs are sort of fun once you get your head around everything that needs to happen in them: pass-thru properties, pass-thru events, save what properties we want in the property bag (so they get saved in the FRM file), create whatever custom properties & events we want.

    For small improvements to existing controls, they work extremely well ... although always a bit more code than just "thinking about it".



    Really, this is closer to a more pure form of subclassing than what we typically call "subclassing" in VB6 (i.e., hooking the message pump).
    Last edited by Elroy; Sep 10th, 2024 at 04:31 PM.
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

  24. #24

    Thread Starter
    Junior Member
    Join Date
    Sep 2024
    Posts
    19

    Re: How do I vertically align the text in a textbox?

    Quote Originally Posted by Elroy View Post
    Ok, I knocked out a UC on my tablet (no mouse, only touch-screen). I think I got all the essentials covered.

    I did make a couple of assumptions though:
    1. Only single-line TextBoxes.
    2. We won't be using any Scrollbars.
    3. Everything will stay in Twips, especially the parent form (or container).

    And, I tested some, but not thoroughly.

    I covered all the most common events and properties, but there are a few I didn't pass through.

    One I can think of is the hWnd to the TextBox. That might be useful in certain instances. Also, I didn't mess with any of the OLE stuff.

    Here's a screenshot:


    Sample project is attached, but here's the code in the custom UC (which is really the only code):

    Code:
    Option Explicit
    '
    Public Enum BS_Special
        txtBsOff = 0&
        txtBsOn = 1&
    End Enum
    '
    Public Enum Appear_Special
        txtAppearFlat = 0&
        txtAppear3D = 1&
    End Enum
    '
    ' Our custom UC events:
    Event Change()
    Event Click()
    Event DblClick()
    Event KeyDown(KeyCode As Integer, Shift As Integer)
    Event KeyPress(KeyAscii As Integer)
    Event KeyUp(KeyCode As Integer, Shift As Integer)
    Event MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    Event MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
    Event MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
    Event Validate(Cancel As Boolean)
    '
    
    
    ' ***************************************************
    '
    ' The properties that affect vertical centering.
    '
    ' ***************************************************
    
    Public Property Set Font(f As StdFont)
        Set txt.Font = f
        AdjustTextBoxPosition
        PropertyChanged
    End Property
    
    Public Property Get Font() As StdFont
        Set Font = txt.Font
    End Property
    
    Public Property Let BorderStyle(bs As BS_Special)
        UserControl.BorderStyle = bs
        AdjustTextBoxPosition
        PropertyChanged
    End Property
    
    Public Property Get BorderStyle() As BS_Special
        BorderStyle = UserControl.BorderStyle
    End Property
    
    Public Property Let Appearance(Appear As Appear_Special)
        UserControl.Appearance = Appear
        AdjustTextBoxPosition
        PropertyChanged
    End Property
    
    Public Property Get Appearance() As Appear_Special
        Appearance = UserControl.Appearance
    End Property
    
    ' ***************************************************
    '
    ' Other properties we'll deal with.
    '
    ' ***************************************************
    
    Public Property Let Alignment(align As AlignmentConstants)
        txt.Alignment = align
        PropertyChanged
    End Property
    
    Public Property Get Alignment() As AlignmentConstants
        Alignment = txt.Alignment
    End Property
    
    Public Property Let BackColor(bc As OLE_COLOR)
        txt.BackColor = bc
        UserControl.BackColor = bc
        PropertyChanged
    End Property
    
    Public Property Get BackColor() As OLE_COLOR
        BackColor = txt.BackColor
    End Property
    
    Public Property Let ForeColor(fc As OLE_COLOR)
        txt.ForeColor = fc
        PropertyChanged
    End Property
    
    Public Property Get ForeColor() As OLE_COLOR
        ForeColor = txt.ForeColor
    End Property
    
    Public Property Let Text(s As String)
        txt.Text = s
        PropertyChanged
    End Property
    
    Public Property Get Text() As String
        Text = txt.Text
    End Property
    
    Public Property Let MaxLength(i As Integer)
        If i < 0& Then i = 0&
        txt.MaxLength = i
        PropertyChanged
    End Property
    
    Public Property Get MaxLength() As Integer
        MaxLength = txt.MaxLength
    End Property
    
    Public Property Let Locked(b As Boolean)
        txt.Locked = b
        PropertyChanged
    End Property
    
    Public Property Get Locked() As Boolean
        Locked = txt.Locked
    End Property
    
    ' ***************************************************
    '
    ' The events we'll raise in our custom UC.
    '
    ' ***************************************************
    
    Private Sub txt_Change()
        RaiseEvent Change
    End Sub
    
    Private Sub txt_Click()
        RaiseEvent Click
    End Sub
    
    Private Sub txt_DblClick()
        RaiseEvent DblClick
    End Sub
    
    Private Sub txt_KeyDown(KeyCode As Integer, Shift As Integer)
        RaiseEvent KeyDown(KeyCode, Shift)
    End Sub
    
    Private Sub txt_KeyPress(KeyAscii As Integer)
        RaiseEvent KeyPress(KeyAscii)
    End Sub
    
    Private Sub txt_KeyUp(KeyCode As Integer, Shift As Integer)
        RaiseEvent KeyUp(KeyCode, Shift)
    End Sub
    
    Private Sub txt_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
        RaiseEvent MouseDown(Button, Shift, X, Y)
    End Sub
    
    Private Sub txt_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
        RaiseEvent MouseMove(Button, Shift, X, Y)
    End Sub
    
    Private Sub txt_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
        RaiseEvent MouseUp(Button, Shift, X, Y)
    End Sub
    
    Private Sub txt_Validate(Cancel As Boolean)
        RaiseEvent Validate(Cancel)
    End Sub
    
    ' ***************************************************
    '
    ' Our UserControl events.
    '
    ' ***************************************************
    
    Private Sub UserControl_ReadProperties(PropBag As PropertyBag)
        '
        ' Ones that affect vertical centering.
        Set txt.Font = PropBag.ReadProperty("Font", txt.Font)
        txt.BorderStyle = PropBag.ReadProperty("BorderStyle", txt.BorderStyle)
        txt.Appearance = PropBag.ReadProperty("Appearance", txt.Appearance)
        '
        ' The rest of them we handle.
        txt.Alignment = PropBag.ReadProperty("Alignment", txt.Alignment)
        txt.BackColor = PropBag.ReadProperty("BackColor", txt.BackColor)
        txt.ForeColor = PropBag.ReadProperty("ForeColor", txt.ForeColor)
        txt.Text = PropBag.ReadProperty("Text", txt.Text)
        txt.MaxLength = PropBag.ReadProperty("MaxLength", txt.MaxLength)
        txt.Locked = PropBag.ReadProperty("Locked", txt.Locked)
    End Sub
    
    Private Sub UserControl_WriteProperties(PropBag As PropertyBag)
        '
        ' Ones that affect vertical centering.
        PropBag.WriteProperty "Font", txt.Font
        PropBag.WriteProperty "BorderStyle", txt.BorderStyle
        PropBag.WriteProperty "Appearance", txt.Appearance
        '
        ' The rest of them we handle.
        PropBag.WriteProperty "Alignment", txt.Alignment
        PropBag.WriteProperty "BackColor", txt.BackColor
        PropBag.WriteProperty "ForeColor", txt.ForeColor
        PropBag.WriteProperty "Text", txt.Text
        PropBag.WriteProperty "MaxLength", txt.MaxLength
        PropBag.WriteProperty "Locked", txt.Locked
    End Sub
    
    Private Sub UserControl_Resize()
        AdjustTextBoxPosition
    End Sub
    
    ' ***************************************************
    '
    ' Private helper procedures.
    '
    ' ***************************************************
    
    Private Sub AdjustTextBoxPosition()
        txt.Left = 0!                           ' This one should be straightforward.
        txt.Width = UserControl.ScaleWidth      ' As well as this one.
        txt.Height = UserControl.ScaleHeight    ' And this one as well.
        '
        ' The Top is the tricky one for vertical centering.
        ' We shouldn't have to worry about borders, as that's handled by ScaleHeight.
        Set UserControl.Font = txt.Font
        Dim fTextHeight As Single
        fTextHeight = UserControl.TextHeight("asdf")
        txt.Top = (UserControl.ScaleHeight - fTextHeight) / 2!
        '
        ' And this seems to be needed to make sure they stay the same.
        UserControl.BackColor = txt.BackColor
    End Sub
    Awesome! Thank you.

  25. #25
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    10,526

    Re: [RESOLVED] How do I vertically align the text in a textbox?

    I did a bit more testing, and the ScaleMode of the container doesn't seem to matter for my custom UC, so I took that assumption out of the above post. It's the ScaleMode of the internal UC's area that matters, which people won't typically be messing with.

    However, the units of the LeftMargin property will always be twips, regardless of the ScaleMode of the form the UC is on.
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

  26. #26
    Fanatic Member
    Join Date
    Aug 2016
    Posts
    691

    Re: [RESOLVED] How do I vertically align the text in a textbox?

    the font height is exactly equal to the text box height, in other words, the font size automatically adapts to the text box size.?

  27. #27
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    10,526

    Re: [RESOLVED] How do I vertically align the text in a textbox?

    Quote Originally Posted by xxdoc123 View Post
    the font height is exactly equal to the text box height, in other words, the font size automatically adapts to the text box size.?
    Is that directed at me? If so, I've got no clue what you're getting at.
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

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