|
-
Jun 10th, 2003, 04:12 PM
#1
Thread Starter
Fanatic Member
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!
-
Jun 10th, 2003, 07:03 PM
#2
PowerPoster
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.

-
Jun 10th, 2003, 07:38 PM
#3
Thread Starter
Fanatic Member
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!
-
Jun 10th, 2003, 07:50 PM
#4
Thread Starter
Fanatic Member
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:
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_GETLINECOUNT = &HBA
Public Function NumLines(txt As TextBox) As Long
NumLines = SendMessage(txt.hwnd, EM_GETLINECOUNT, 0&, ByVal 0&)
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!
-
Jun 11th, 2003, 03:27 AM
#5
Frenzied Member
Isn't there a property for the text box to have all the text centered?
-
Jun 11th, 2003, 09:22 AM
#6
Thread Starter
Fanatic Member
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:
Option Explicit
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_GETLINECOUNT = &HBA
Private Const TEXT_HEIGHT = 210
Private Sub UserControl_Resize()
ResizeControls
End Sub
Private Sub txtDescription_GotFocus()
'hightlight text
txtDescription.SelStart = 0
txtDescription.SelLength = Len(txtDescription.Text)
'make description full size while it has focus
ResizeDescription
End Sub
Private Sub txtDescription_LostFocus()
'make sure beginning text is visible
txtDescription.SelStart = 0
txtDescription.SelLength = 0
ResizeControls
End Sub
Private Sub ResizeDescription()
txtDescription.Top = UserControl.ScaleTop
txtDescription.Width = UserControl.ScaleWidth - 60
txtDescription.Left = 30
txtDescription.Height = txtUtensil.Top - txtDescription.Top
End Sub
'WARNING! DO NOT REMOVE OR MODIFY THE FOLLOWING COMMENTED LINES!
'MappingInfo=UserControl,UserControl,-1,Refresh
Public Sub Refresh()
UserControl.Refresh
ResizeControls
End Sub
Private Sub ResizeControls()
txtUtensil.Left = 50
txtUtensil.Top = UserControl.ScaleTop + UserControl.ScaleHeight - txtUtensil.Height - 5
txtUtensil.Height = UserControl.TextHeight(txtUtensil.Text) - 50
txtUtensil.Width = UserControl.ScaleWidth - 100
txtDescription.Width = UserControl.ScaleWidth - 60
txtDescription.Left = 30
txtDescription.Height = GetLines(txtDescription) * TEXT_HEIGHT
txtDescription.Top = (txtUtensil.Top - txtDescription.Height) / 2
End Sub
Private Function GetLines(txt As TextBox) As Long
GetLines = SendMessage(txt.hwnd, EM_GETLINECOUNT, 0&, ByVal 0&)
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!
-
Jun 11th, 2003, 09:38 AM
#7
Frenzied Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|