Solved

Center text top to bottom in textbox. How to?

Posted on 1998-06-17
8
192 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
Comment Utility
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
Comment Utility
Did you check this code? It is not working under VB5.
0
 
LVL 18

Expert Comment

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

Expert Comment

by:deighton
Comment Utility
Yes I ran it, but in VB4 - where is the problem? What goes wrong.  Please advise.
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 18

Expert Comment

by:deighton
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Nice job! Thanks for your help.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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 …
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…
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…
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…

772 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now