Solved

Center text top to bottom in textbox. How to?

Posted on 1998-06-17
8
200 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
[X]
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
  • 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
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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

728 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