簡體   English   中英

c#帶有彩色文本的Console.WriteLine()正在集中輸出

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM