简体   繁体   English

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

[英]c# Console.WriteLine() with color text is massing the output

I am trying to show a warning to the user by the following code. 我试图通过以下代码向用户显示警告。 But it is massing the output text. 但它正在集中输出文本。

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);

The display looks as follows:- 显示如下: - 在此输入图像描述

I only want to make coloring of the text "The selected row is not found in the database." 我只想对文本着色“在数据库中找不到所选行”。 . Nothing extra. 没什么特别的。 Otherwise it looks ugly. 否则它看起来很难看。 How to do it? 怎么做?

The problem is that the carriage return and new line draws the background color for these lines. 问题是回车和新线为这些线条绘制背景颜色。 Simply use Console.Write() instead of Console.WriteLine() : 只需使用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