Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Center text top to bottom in textbox. How to?

Posted on 1998-06-17
8
197 Views
Last Modified: 2010-05-03
I want to center text strings from a database in a multiline textbox. Not only the usual left-right centering, but also top to bottom (the text must be centered in the middle of the textbox).
How can this be done?
0
Comment
Question by:Guiseppe
  • 5
  • 3
8 Comments
 
LVL 18

Accepted Solution

by:
deighton earned 50 total points
ID: 1463532
Private Sub add_align(sString() As String, tb As TextBox, nl As Integer)
    'sString an array holding each line of text (option based at 0)
    'tb Your text box (multi line and text centred)
    'nl  - the maximum number of lines visible in the text box
   
    Dim i As Integer
   
    While sString(i) <> ""
        i = i + 1
    Wend
   
   
    iBlanks = (nl - i + 1) \ 2
       
    For c = 1 To iBlanks
        tb = tb + vbCrLf
    Next
   
    For c = 0 To i - 1
        tb = tb + sString(c) + vbCrLf
    Next
   
   
End Sub

'EXAMPLE CALL

Private Sub Command2_Click()
    Dim strings(20) As String
    strings(0) = "Shearer"
    strings(1) = "Sherringham"
    strings(2) = "Adams"
    strings(3) = "Seaman"
    strings(4) = "Neville"
    Call add_align(strings, Text1, 12)
End Sub


0
 

Author Comment

by:Guiseppe
ID: 1463533
Did you check this code? It is not working under VB5.
0
 
LVL 18

Expert Comment

by:deighton
ID: 1463534
Yes I ran it, but in VB4 - where is the problem? What goes wrong.  Please advise.
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 18

Expert Comment

by:deighton
ID: 1463535
Yes I ran it, but in VB4 - where is the problem? What goes wrong.  Please advise.
0
 
LVL 18

Expert Comment

by:deighton
ID: 1463536
You might need to replace

do while sString(i) <> ""
            i = i + 1
loop
     
I assume you created the text box on your form - mine is big enoug to display 12 lines .

Did it compile?  If no what error?


0
 

Author Comment

by:Guiseppe
ID: 1463537
Works fine now. There must have been something wrong with my computer. He didn't accept the array definition. I rebuilt the project.

One more question: now you put 12 lines in the textbox. Is there a way to check at runtime how many lines there are in a textbox and then adjust the text with your procedure?

I'll increase the points to 50 for this extra answer.
0
 
LVL 18

Expert Comment

by:deighton
ID: 1463538
Heres a quick improvement I thought up.

Private Sub add_align(sString() As String, tb As TextBox)
    'sString an array holding each line of text (option based at 0)
    'tb Your text box (multi line and text centred)
    'nl  - the maximum number of lines visible in the text box
   
    Dim i As Integer
    Dim nl As Integer
    Dim dScaleFactor As Double
   
    dScaleFactor = 211
   
    'I got 211 because my text box height is 2535
    'and 2535 / 12 = 211.
    'For a given text box get its height from the
    'properties and then divide it by the number of
    'lines it accomodates. This gives a value that
    'should work in your system
   
    nl = tb.Height \ dScaleFactor
   
    While sString(i) <> ""
        i = i + 1
    Wend
   
   
    iBlanks = (nl - i + 1) \ 2
       
    For c = 1 To iBlanks
        tb = tb + vbCrLf
    Next
   
    For c = 0 To i - 1
        tb = tb + sString(c) + vbCrLf
    Next
   
   
End Sub

0
 

Author Comment

by:Guiseppe
ID: 1463539
Nice job! Thanks for your help.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Paint/Redraw window while dragging 16 82
How to incorporate an error traping routing to existing code in VB6 11 45
MsgBox 4 68
Error with a code discussed on this page 5 29
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question