Results 1 to 7 of 7

Thread: Center Textbox Vertically **Resolved ** Kind of

  1. #1

    Thread Starter
    Fanatic Member Armbruster's Avatar
    Join Date
    Sep 2002
    Location
    Maryland Heights, MO
    Posts
    857

    Unhappy Center Textbox Vertically **Resolved ** Kind of

    I have a custom control with a multiline text box (scrollbars set to false). The text box resizes with the control. The user can enter text at runtime.

    When the textbox loses focus, I would like to center the text vertically. I can't think of any way to do this. I searched the forums and couldn't find any examples either.

    Anyone ever done this or have a clever workaround?
    Last edited by Armbruster; Jun 10th, 2003 at 07:51 PM.
    "Look! Up in the sky! It's a bird! It's a plane! It's Diaper-Head Boy! (there by my name!) Yes, Diaper-Head Boy, who disguised as my son, Seth, fights a never-ending battle for truth, justice and terrorizing my house!

    Resistance is futile, you will be compiled . . . Please!

  2. #2
    PowerPoster Arc's Avatar
    Join Date
    Sep 2000
    Location
    Under my rock
    Posts
    2,336
    I'm confused. The textbox is the custom control? The textbox is on the custom control?

    The user can center text in what? The custom control or the textbox? What is this custom control? What does it do?

    I doubt anyone can answer your question without some code and details of this "custom control".
    -We have enough youth. How about a fountain of "Smart"?
    -If you can read this, thank a teacher....and since it's in English, thank a soldier.


  3. #3

    Thread Starter
    Fanatic Member Armbruster's Avatar
    Join Date
    Sep 2002
    Location
    Maryland Heights, MO
    Posts
    857
    The textbox is on the custom control. The user control is a pretty simple control. A userform with 2 textboxes.

    I guess I really just need to know if it is possible to center text vertically in a multiline text box. The reason I mentioned the custom control is to explain that the textbox's size can change.
    "Look! Up in the sky! It's a bird! It's a plane! It's Diaper-Head Boy! (there by my name!) Yes, Diaper-Head Boy, who disguised as my son, Seth, fights a never-ending battle for truth, justice and terrorizing my house!

    Resistance is futile, you will be compiled . . . Please!

  4. #4

    Thread Starter
    Fanatic Member Armbruster's Avatar
    Join Date
    Sep 2002
    Location
    Maryland Heights, MO
    Posts
    857
    Hey Arc,

    Thanks for responding. I've been searching all day and couldn't find a single example of vertically centering text in a textbox.

    I figured I could use the sendmessage api like . . .

    VB Code:
    1. Private Declare Function SendMessage Lib "user32" _
    2.    Alias "SendMessageA" _
    3.   (ByVal hwnd As Long, _
    4.    ByVal wMsg As Long, _
    5.    ByVal wParam As Long, _
    6.    lParam As Any) As Long
    7.  
    8. Private Const EM_GETLINECOUNT = &HBA
    9.  
    10.  
    11. Public Function NumLines(txt As TextBox) As Long
    12.     NumLines = SendMessage(txt.hwnd, EM_GETLINECOUNT, 0&, ByVal 0&)
    13. End Function

    to get the number of lines of text in the text box. The nice thing about the using the api method is that it will get the "true" number of lines in the textbox (if you type a line of text and it wraps, it will count the wrapped line as a new line).

    I figured I could then resize the text box based on it's parent's .textheight property so that the textbox is only tall enought to fit the text it contains. Then I can center the actual textbox control instead of centering the text it contains.

    A little ugly, but since I couldn't find another way, I guess it will have to do!
    "Look! Up in the sky! It's a bird! It's a plane! It's Diaper-Head Boy! (there by my name!) Yes, Diaper-Head Boy, who disguised as my son, Seth, fights a never-ending battle for truth, justice and terrorizing my house!

    Resistance is futile, you will be compiled . . . Please!

  5. #5
    Frenzied Member Spajeoly's Avatar
    Join Date
    Mar 2003
    Location
    Utah
    Posts
    1,068
    Isn't there a property for the text box to have all the text centered?

  6. #6

    Thread Starter
    Fanatic Member Armbruster's Avatar
    Join Date
    Sep 2002
    Location
    Maryland Heights, MO
    Posts
    857
    Originally posted by Spajeoly
    Isn't there a property for the text box to have all the text centered?
    Horizontally? Yes
    Vertically? No

    I wrote some code this morning to center the text box itself instead of the text. I've been testing it for a bit and so far I really like the results. The textbox is borderless, so it looks to the end user like the text just centers when they move off the text box.

    VB Code:
    1. Option Explicit
    2.  
    3. Private Declare Function SendMessage Lib "user32" _
    4.    Alias "SendMessageA" _
    5.   (ByVal hwnd As Long, _
    6.    ByVal wMsg As Long, _
    7.    ByVal wParam As Long, _
    8.    lParam As Any) As Long
    9.  
    10. Private Const EM_GETLINECOUNT = &HBA
    11. Private Const TEXT_HEIGHT = 210
    12.  
    13. Private Sub UserControl_Resize()
    14.     ResizeControls
    15. End Sub
    16. Private Sub txtDescription_GotFocus()
    17.     'hightlight text
    18.     txtDescription.SelStart = 0
    19.     txtDescription.SelLength = Len(txtDescription.Text)
    20.     'make description full size while it has focus
    21.     ResizeDescription
    22. End Sub
    23. Private Sub txtDescription_LostFocus()
    24.     'make sure beginning text is visible
    25.     txtDescription.SelStart = 0
    26.     txtDescription.SelLength = 0
    27.     ResizeControls
    28. End Sub
    29. Private Sub ResizeDescription()
    30.     txtDescription.Top = UserControl.ScaleTop
    31.     txtDescription.Width = UserControl.ScaleWidth - 60
    32.     txtDescription.Left = 30
    33.     txtDescription.Height = txtUtensil.Top - txtDescription.Top
    34. End Sub
    35.  
    36. 'WARNING! DO NOT REMOVE OR MODIFY THE FOLLOWING COMMENTED LINES!
    37. 'MappingInfo=UserControl,UserControl,-1,Refresh
    38. Public Sub Refresh()
    39.     UserControl.Refresh
    40.     ResizeControls
    41. End Sub
    42. Private Sub ResizeControls()
    43.    
    44.     txtUtensil.Left = 50
    45.     txtUtensil.Top = UserControl.ScaleTop + UserControl.ScaleHeight - txtUtensil.Height - 5
    46.     txtUtensil.Height = UserControl.TextHeight(txtUtensil.Text) - 50
    47.     txtUtensil.Width = UserControl.ScaleWidth - 100
    48.    
    49.     txtDescription.Width = UserControl.ScaleWidth - 60
    50.     txtDescription.Left = 30
    51.     txtDescription.Height = GetLines(txtDescription) * TEXT_HEIGHT
    52.     txtDescription.Top = (txtUtensil.Top - txtDescription.Height) / 2
    53.  
    54. End Sub
    55. Private Function GetLines(txt As TextBox) As Long
    56.     GetLines = SendMessage(txt.hwnd, EM_GETLINECOUNT, 0&, ByVal 0&)
    57. End Function
    "Look! Up in the sky! It's a bird! It's a plane! It's Diaper-Head Boy! (there by my name!) Yes, Diaper-Head Boy, who disguised as my son, Seth, fights a never-ending battle for truth, justice and terrorizing my house!

    Resistance is futile, you will be compiled . . . Please!

  7. #7
    Frenzied Member Spajeoly's Avatar
    Join Date
    Mar 2003
    Location
    Utah
    Posts
    1,068
    Crap, I need to get more sleep before i go tackling such difficult questions I guess lol.

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