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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Centering Text using MsgBox

Status
Not open for further replies.

Bloobird

Programmer
Oct 23, 2001
49
GB
Hi - I was wondering of there was a way of centering the text displayed when using Msgbox, for a particularly long message that goes over more than one line, as currently it appears left justified and looks untidy

Thanks
 
I don't think there is so maybe you should consider making your own message box to perform the same task.
 
I dunno if this'll help or not, but a trick I learned that makes it easy to make a multiline messagebox prompt look nice is to break the lines up yourself like this:
-----------------------
Dim Msg As String

Msg = "Tomorrow and tomorrow and tomorrow" & vbCrLf & _
"creeps this petty pace from day to" & vbCrLf & _
"day, to the last syllable of recorded" & vbCrLf & _
"time."

MsgBox Msg, vbInformation, "Macbeth"
-----------------------
This way you can line up the text in code, and it'll line up pretty close in the message box.

Good luck!

~Mike
Any man willing to sacrifice liberty for security deserves neither liberty nor security.

-Ben Franklin
 
that works Mike, had to write it this way in order for it to work....

Dim msg As String

MsgBox "Tomorrow and tomorrow and tomorrow" & vbCrLf _
& "creeps this petty pace from day to" & vbCrLf _
& "day,to the last syllable of recorded" & vbCrLf _
& "time."""

MsgBox msg, vbInformation, "Macbeth"

;-)
 
Thanks Mike, the message looks a lot better now. I've also padded some text with spaces to give the appearance of being centered, not perfect, but it does the job
 
Try this approach. It is only a demo, and it uses TRICKERY, so it may not be suitable for every purpose. It is also in-exact for the centering (partially from the trickery). Some brave conder may (easily) be able to improve on this - please let me know what enhancements you come up with.


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

    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

Code:
Public Function basTstCentMsg()

    Dim MyStr As String
    Dim MyWidth As Single
    Dim MyRtn As Boolean

    MyStr = &quot;Tomorrow and tomorrow and tomorrow &quot;
    MyStr = MyStr & &quot;creeps this petty pace from day to day, &quot;
    MyStr = MyStr & &quot;to the last syllable of recorded time.&quot;
    MyWidth = 2.5


    MyRtn = basCentTextMsg(MyStr, MyWidth)
End Function
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