简体   繁体   中英

How to display text in message box on different lines?

I want each Total displayed on a different line in the box but at the moment it keep overlapping. Could someone please show me the way?

    private void SummaryButton_Click(object sender, EventArgs e)
    {
        TotalMoniesTaken = AmountDue + TotalMoniesTaken;
        TotalGuests = NumberOfGuests + TotalGuests;
        TotalLunchBookings = NumberOfGuests + TotalLunchBookings;
        TotalEarlyBookings = NumberOfGuests + TotalEarlyBookings;
        TotalLateBookings = NumberOfGuests + TotalLateBookings;
        TotalCornerTables = NumberOfGuests + TotalCornerTables;
        TotalWaiters = NumberOfGuests + TotalWaiters;

        MessageBox.Show("Total Monies Taken is €" + TotalMoniesTaken + 
                        "Total Number of Bookings = " + TotalGuests + 
                        "Total Lunch Bookings = " + TotalLunchBookings + 
                        "Total Early Bookings = " + TotalEarlyBookings +
                        "Total Late Bookings = " + TotalLateBookings + 
                        "Total Corner Tables = " + TotalCornerTables +
                        "Total Waiters = " + TotalWaiters);






    }

Displaying this doesn't include a new line:

"Total Monies Taken is €" + TotalMoniesTaken

But this does:

"Total Monies Taken is €" + TotalMoniesTaken + Environment.NewLine

Add newlines to each one :

"Total Monies Taken is €" + TotalMoniesTaken + Environment.NewLine
"Total Number of Bookings = " + TotalGuests + Environment.NewLine

..etc

There are a few ways that you can do this in C#, firstly you can use newlines in your string

MessageBox.Show("Total Monies Taken is €" + TotalMoniesTaken + 
                    "\nTotal Number of Bookings = " + TotalGuests + 
                    "\nTotal Lunch Bookings = " + TotalLunchBookings + 
                    "\nTotal Early Bookings = " + TotalEarlyBookings +
                    "\nTotal Late Bookings = " + TotalLateBookings + 
                    "\nTotal Corner Tables = " + TotalCornerTables +
                    "\nTotal Waiters = " + TotalWaiters);

Alternatively, you could use Environment.NewLine

MessageBox.Show("Total Monies Taken is €" + TotalMoniesTaken + Environment.NewLine + 
                    "Total Number of Bookings = " + TotalGuests + Environment.NewLine + 
                    "Total Lunch Bookings = " + TotalLunchBookings + Environment.NewLine + 
                    "Total Early Bookings = " + TotalEarlyBookings + Environment.NewLine + 
                    "Total Late Bookings = " + TotalLateBookings + Environment.NewLine + 
                    "Total Corner Tables = " + TotalCornerTables + Environment.NewLine +
                    "Total Waiters = " + TotalWaiters);

只需在每行末尾使用\\ n

MessageBox.Show("test1 \n test2 \n test3");

You can do two ways:

1st one with \\n:

Eg: string msg = "text\nwith two lines";

2nd way with Environment.NewLine:

Eg:string msg = "Text " + Environment.NewLine + "with two lines";

一些新的C#功能(未经测试) MessageBox.Show( $@"Total Monies Taken is €{TotalMoniesTaken} Total Number of Bookings = {TotalGuests} ...");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM