[英]c# Console.WriteLine() with color text is massing the output
我試圖通過以下代碼向用戶顯示警告。 但它正在集中輸出文本。
Console.ForegroundColor = ConsoleColor.Yellow;
Console.BackgroundColor = ConsoleColor.Red;
Console.WriteLine($"The selected row {selection} is not found in the database.");
Console.ResetColor();
Console.ReadLine();
Environment.Exit(0);
我只想對文本着色“在數據庫中找不到所選行”。 。 沒什么特別的。 否則它看起來很難看。 怎么做?
問題是回車和新線為這些線條繪制背景顏色。 只需使用Console.Write()
而不是Console.WriteLine()
:
Console.ForegroundColor = ConsoleColor.Yellow;
Console.BackgroundColor = ConsoleColor.Red;
// only write without new line
Console.Write($"The selected row {selection} is not found in the database.");
Console.ResetColor();
Console.WriteLine(); // if necessary
Console.ReadLine();
Environment.Exit(0);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.