?
Solved

Center text top to bottom in textbox. How to?

Posted on 1998-06-17
8
Medium Priority
?
209 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 200 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Question has a verified solution.

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

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Suggested Courses
Course of the Month15 days, 19 hours left to enroll

850 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