MessageBox Center Text?

Posted on 2006-06-08
Last Modified: 2007-12-19
Q. How do I center the text in a MessageBox?

MessageBox.Show("UserName and Password:" + "\n\n" + str_UserNamePass.ToString() + "\n\n" +
                            "Please keep in safe place!", Text.Center???);            
Question by:kvnsdr
    LVL 25

    Accepted Solution

    I don't htink you can do this with the standard messagbox - it's just a hook into the old WinAPI MessageBox, which doesn;t allow formatting AFAIK.

    Two options come to mind:

    Prepend your message with spaces to "center" the text - you may have to compute how many spaces to use for varialbe length lines.

    Create your own messagebox, with a label, icon, and buttons.  That way you could center the label (or do any other formatting you want).  If you wanted to get really fancy, you could even support all of the same parameters as MessageBox.Show(), including dynamic buttons, icons, etc.

    Here's an examplt in VB.NET:,7,10,9

    and Here's one that's already built:
    LVL 69

    Assisted Solution

    by:Éric Moreau
    Hi dstanley9,

    > Here's an examplt in VB.NET:

    That's a very good article ;) Check the author name and the nickname for this comment for any similarity!

    LVL 25

    Expert Comment

    Anything I can do to help :)

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Join & Write a Comment

    Article by: Ivo
    Anonymous Types in C# by Ivo Stoykov Anonymous Types are useful when  we do not need to follow usual work-flow -- creating object of some type, assign some read-only values and then doing something with them. Instead we can encapsulate this read…
    Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
    This video discusses moving either the default database or any database to a new volume.
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

    745 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

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now