Solved

Center text top to bottom in textbox. How to?

Posted on 1998-06-17
8
195 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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
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…

773 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