Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Centering text in a Message Box

Status
Not open for further replies.

Jackie

MIS
Feb 9, 2000
148
US
Is it possible to center the text displayed in a message box? If so, what is the syntax of the msgbox() function?

Thanks in advance.
 
NOTE:[/][/][/]

The following code procedure is NOT suitable for multiuser apps. It is not a "good" way to do the overall process. The "better" approach would probably just return a string array to a calling proceudre to use in the nessage box.


Code:
Public Function basCentTextMsg(strIn As String, LineLen As Single) As Boolean

    'Michael Red 12/11/2001
    'Elaborate way to center text lines in a msg box

    'strIn is the (Entire) text
    'LineLn is the Width (IN INCHES)
    'to center the line(s) within

    Dim strLen As Integer
    Dim Idx As Integer
    Dim MySpace As Single
    Dim MyWords As Variant
    Dim WdLen() As Single
    Dim MyLine As String
    Dim MyText As String
    Dim blnStrtLn As Boolean

    Printer.ScaleMode = vbInches

    MyWords = Split(strIn, " ")     'Create an Array of WORDs from input
    ReDim WdLen(UBound(MyWords))    'Array of Word Lengths
    
    'Get the length of Each WORD, WITH the Trailing Space
    Idx = 0
    Do While Idx <= UBound(MyWords)
        MyWords(Idx) = MyWords(Idx) & &quot; &quot;
        WdLen(Idx) = Printer.TextWidth(MyWords(Idx))
        Idx = Idx + 1
    Loop

    Idx = 0
    Do While Idx <= UBound(MyWords)
        If (Printer.TextWidth(Trim(MyLine & MyWords(Idx))) <= LineLen) Then
            MyLine = MyLine & MyWords(Idx)
            blnStrtLn = True
         Else
            MySpace = LineLen - Printer.TextWidth(Trim(MyLine))
            NSpaces = (MySpace / Printer.TextWidth(&quot; &quot;)) / 2
            MyText = MyText & Space(NSpaces) & Trim(MyLine) & Space(NSpaces) & vbCrLf
            blnStrtLn = False
            MyLine = MyWords(Idx)
        End If

        Idx = Idx + 1
    Loop

    If (blnStrtLn = True) Then
        MySpace = LineLen - Printer.TextWidth(Trim(MyLine))
        NSpaces = (MySpace / Printer.TextWidth(&quot; &quot;)) / 2
        MyText = MyText & Space(NSpaces) & Trim(MyLine) & Space(NSpaces) & vbCrLf
        blnStrtLn = False
        MyLine = &quot;&quot;
    End If

    MsgBox MyText, vbOKOnly, &quot;Centered Text MsgBox&quot;

End Function
MichaelRed
m.red@att.net

There is never time to do it right but there is always time to do it over
 
Thank you. Do you have an example of the &quot;better&quot; way that you mentioned?

Jackie
 
No, this was generated in response to a previous post / request. It is just an &quot;exercise&quot;, to illustrate the approach. To do it &quot;properly&quot;, you would create a string array, and populate it with the individual lines (i.e. wherever I included a vbCrLf, you would add an item to the string array. Return the array as a variant, and have the calling program &quot;concatenate&quot; the elements of the array into a single &quot;string&quot; (be sure to incl the vbCrLf between each element) an send this to the msgbox.

MichaelRed
m.red@att.net

There is never time to do it right but there is always time to do it over
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top