Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

center text verytically in textbox

Status
Not open for further replies.

sal21

Programmer
Apr 26, 2004
429
IT
center text vertically in textbox, possible?

now i see
____
ciao

____


i need

___

ciao

_____

 
Do you think it might be helpful for you to identify the application?
 
In VBA it would be Object.ShapeRange.TextFrame2.VerticalAnchor = msoAnchoMiddle

I'd suspect something similar in VB6.
 
?I'd suspect something similar in VB6.

I wouldn't

VB 6 textboxes do not have a vertical alignment property

But you can fake it with a couple of API calls. Note as ever that this is an illustrative idea, NOT a full, production solution

Code:
Option Explicit

[COLOR=green]'Set MultiLine property of the TextBox to True[/color]

Private Type RECT
        Left   As Long
        Top   As Long
        Right   As Long
        Bottom   As Long
End Type

Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long

Private Const EM_GETRECT = &HB2
Private Const EM_SETRECTNP = &HB4

Sub VerMiddleText(mText As TextBox)
        If mText.MultiLine = False Then Exit Sub
        Dim rc As RECT
        Dim tmpTop As Long
        Dim tmpBot As Long
        
        SendMessage mText.hwnd, EM_GETRECT, 0, rc
        Set Me.Font = mText.Font
        tmpTop = ((rc.Bottom - rc.Top) - (mText.Parent.TextHeight("H ") \ Screen.TwipsPerPixelY)) \ 2 + 2
        tmpBot = ((rc.Bottom - rc.Top) + (mText.Parent.TextHeight("H ") \ Screen.TwipsPerPixelY)) \ 2 + 2
        rc.Top = tmpTop
        rc.Bottom = tmpBot
        mText.Alignment = vbCenter
        SendMessage mText.hwnd, EM_SETRECTNP, 0&, rc
        mText.Refresh
End Sub

Private Sub Form_Load()
        VerMiddleText Text1
End Sub



 
dubt, this code work..
see image.

SI and NO are label....

Code:
Option Explicit
Private Type RECT
        Left   As Long
        Top   As Long
        Right   As Long
        Bottom   As Long
End Type
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long

Private Const EM_GETRECT = &HB2
Private Const EM_SETRECTNP = &HB4
Private Sub Form_Activate()

    Me.Text1.SetFocus

End Sub
Private Sub Form_Load()

    Call STOP_MOVE(Me.Caption)

    Me.TSCONTRINO.Text = SCONTRINO
    
    VerMiddleText Me.TSCONTRINO

End Sub
Private Sub LSI_Click()

    NOLOAD1 = True
    OK = True
    Unload Me

End Sub
Private Sub LNO_Click()

    NOLOAD1 = True
    OK = False
    Unload Me

End Sub
Sub VerMiddleText(mText As TextBox)
        If mText.MultiLine = False Then Exit Sub
        Dim rc As RECT
        Dim tmpTop As Long
        Dim tmpBot As Long
        
        SendMessage mText.hwnd, EM_GETRECT, 0, rc
        Set Me.Font = mText.Font
        tmpTop = ((rc.Bottom - rc.Top) - (mText.Parent.TextHeight("H ") \ Screen.TwipsPerPixelY)) \ 2 + 2
        tmpBot = ((rc.Bottom - rc.Top) + (mText.Parent.TextHeight("H ") \ Screen.TwipsPerPixelY)) \ 2 + 2
        rc.Top = tmpTop
        rc.Bottom = tmpBot
        mText.Alignment = vbCenter
        SendMessage mText.hwnd, EM_SETRECTNP, 0&, rc
        mText.Refresh
End Sub
 
 https://files.engineering.com/getfile.aspx?folder=99c644d1-5da6-4cff-a245-da34012f3ee2&file=SCO.jpg
I suspect that ypu've ignored this key requirement that was a comment I specifically put in the code:

[tt]Set MultiLine property of the TextBox to True[/tt]
 
SORRY me strongm, i'm distract...

now work perfect!
Tks.


but i can use also for label SI an NO, or i need to use another method
 
i need to use another method" control. Use Command Buttons.

strongm said:
this
Code:
 is an illustrative idea, NOT a full, production solution[/quote]

"now work perfect!" - not really.  Type anything in the text box, hit Enter, type something else, hit Enter.  

---- Andy

[i]"Hmm...they have the internet on computers now"--Homer Simpson[/i]
 
Why even bother with the text vertical alignment of the text box?
You can accomplish that very easy with assigning right Height of the text box:

TextBox_qkqina.png


Actually, between 'too small' and 'just right' there is almost no difference.
And what is nice, you really cannot make the textbox 'too small' because VB will not allow you to...

---- Andy

"Hmm...they have the internet on computers now"--Homer Simpson
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top