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

How do I center justify in a MsgBox? 3

Status
Not open for further replies.

Petemush

Technical User
Jun 21, 2002
255
GB
Quite simple, the subject line!

Can it be done, or is it impossible?

Cheers,

Pete
 
I know of no way to center the text in a message box by way of a command or property setting.

However, you can force the text in a message box to break onto another line by using the "Chr Function” (search “Chr Function” in Access’s on line help).

Example:
MsgBox "This is the first short line of text" & Chr (13) & _
"and this is the 2nd short line on text" & Chr (10) & _
"and this is the third long line of text in my msgbox."

The "Chr (13)" is the carriage return in ASCII and Chr (10) is the line feed, which will also break the text onto the next line.

If you want to add blank spaces to the leading edge your text and maybe use the Chr function to force a line break, you should be able to get the centered look you're hoping for.

Example:
MsgBox "Left" & Chr (13) & _
" Centered" & Chr (10) & _
" Right"

Also, for those readers who might not know this, the blank space followed by the underscore at the end of these lines of code are themselves “line breaks” or “line continuations” in VBA and used to make the code more readable.

Hope this helps.

Tom
 
Cheers, I'm already using the cariage returns to seperate lines and would use blank spaces for a more centered look but I'm using a variable in the MsgBox which constantly changes length.

Thanks anway.

Pete
 
It might be more work than needed but you could find the length of your variable, len(myVariable), and then have your code add the proper number of leading edge blank spaces as another variable and then concatenate them together in your msgbox. But this is likely more bother than is need.


Happy New Year
Tom



 
Just for reference, visual basic has built-in constants for carriage returns & line breaks if you don't want to remember the character numbers.

vbCr
vbLf
vbCrLf
 
Public Function CenterIt(strText As String, _
intLen As Integer) As String
Dim strTemp As String
Dim intTemp As Integer
strTemp = Trim$(strText)
If Len(strTemp) >= intLen Then 'Best we can do
CenterIt = strTemp
Exit Function
End If
intTemp = (intLen - Len(strTemp)) \ 2 'Integer division
CenterIt = Space$(intTemp) & strTemp
End Function

You may want to play around with the message boxes you are using. If they are all about the same size, you could make the second argument optional and set it equal to a default value to save a little coding like so:

etc, Optional intLen As Integer = 30)

Good Luck!

PS. If you know all of this already, sorry to bore you with the details.



 
Unless you switch to a fixed width font, character padding will not accomplish the centering -at least not as presented above.


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

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

Searching for employment in all the wrong places
 
Thanks to everyone for their suggestions, very helpful!

Micheal: I tried to use your code but Printer.ScaleMode doesn't work since Access 2K thinks that Printer is a variable. Am I missing a reference or has something changed from earlier versions of access. Scalemode and TextWidth both exist as a reports property and method respectively but I'm not sure how to utilise them.

Thanks again everyone.

Pete
 
I do not hava ver 2k2, so do not know what changes make it inoperable.

MichaelRed
m.red@att.net

Searching for employment in all the wrong places
 
Not sure if it was a typo but I'm using Acess 2000, not 2002 like I think you wrote.

Cheers,

Pete
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top