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 SkipVought 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 MsgBox 1

Status
Not open for further replies.

jlindquist

Programmer
Jun 8, 2001
14
0
0
US
Any easy way to center text when you call MsgBox "Something",vbButtons,"Whatever"...blah blah blah
 
No easy way. I'm sure someone has worked something someway. I've created a message wrap but not centered text.

Steve King Growth follows a healthy professional curiosity
 
Steve,
HOw did you accomplish the message wrap?

Thanks.
 
Msgbox "First line" & vbCrLf & "Second line"
 
Did this one last month for SOMEONE? just noticed this post.


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
 
Bravo, Michael! Bravo!

Joe Miller
joe.miller@flotech.net
 

Michael,

I haven't tried your code out yet but you deserve a star for that one .
ZaZa
 
Thank you, Thank you, Thank you. Kudos and job offers welcome.


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