交替控制台背景颜色 - C#

时间:2017-06-15 17:36:23

标签: c# .net colors console console-application

我目前正在尝试创建一个输出某些数据集的程序。数据看起来像这样:

 Offer 1:          Economy        $203.20         Jetblue Airways
    Option 1
    Departure: 2017-12-25 - 20:29:00.000             From: JFK               Arrival: 2017-12-25 - 23:06:00.000              At: JAX
    Departure: 2017-12-26 - 07:00:00.000             From: JAX               Arrival: 2017-12-26 - 08:16:00.000              At: FLL
    Departure: 2017-12-26 - 09:14:00.000             From: FLL               Arrival: 2017-12-26 - 11:14:00.000              At: PAP
    Total projected travel time: 14 Hours, 45 Minutes

    Option 2
    Departure: 2017-12-25 - 16:41:00.000             From: JFK               Arrival: 2017-12-25 - 19:14:00.000              At: JAX
    Departure: 2017-12-26 - 07:00:00.000             From: JAX               Arrival: 2017-12-26 - 08:16:00.000              At: FLL
    Departure: 2017-12-26 - 09:14:00.000             From: FLL               Arrival: 2017-12-26 - 11:14:00.000              At: PAP
    Total projected travel time: 18 Hours, 33 Minutes

    Offer 2:          Economy        $216.60         Jetblue Airways
    Option 1
    Departure: 2017-12-25 - 05:45:00.000             From: EWR               Arrival: 2017-12-25 - 08:28:00.000              At: MCO
    Departure: 2017-12-25 - 09:55:00.000             From: MCO               Arrival: 2017-12-25 - 12:07:00.000              At: PAP
    Total projected travel time: 6 Hours, 22 Minutes

    Option 2
    Departure: 2017-12-25 - 20:30:00.000             From: EWR               Arrival: 2017-12-25 - 23:13:00.000              At: MCO
    Departure: 2017-12-26 - 09:55:00.000             From: MCO               Arrival: 2017-12-26 - 12:07:00.000              At: PAP
    Total projected travel time: 15 Hours, 37 Minutes

    Offer 3:          Economy        $218.60         Jetblue Airways
    Option 1
    Departure: 2017-12-25 - 06:00:00.000             From: JFK               Arrival: 2017-12-25 - 08:48:00.000              At: MCO
    Departure: 2017-12-25 - 09:55:00.000             From: MCO               Arrival: 2017-12-25 - 12:07:00.000              At: PAP
    Total projected travel time: 6 Hours, 7 Minutes

    Option 2
    Departure: 2017-12-25 - 21:45:00.000             From: JFK               Arrival: 2017-12-26 - 00:34:00.000              At: MCO
    Departure: 2017-12-26 - 09:55:00.000             From: MCO               Arrival: 2017-12-26 - 12:07:00.000              At: PAP
    Total projected travel time: 14 Hours, 22 Minutes

可以有多达50个商品,因此您可以想象输出可能会变得相当长。我想知道是否有任何可能的方法以编程方式格式化背景颜色,使每个选项在两个颜色块之间交替返回和第四。例如,如果选项1具有灰色背景,则选项2将具有黑色背景,选项3灰色,选项4黑色等...我不关心优惠文本被包围的颜色,只要它与之前的颜色块相反。我尝试使用Console.BackgroundColor设置背景颜色,但它只突出显示文本,使控制台保持原始颜色。我发现你可以使用Console.BackgroundColor(),然后使用Console.Clear将整个控制台设置为一种颜色,但这会删除我的情况下的数据。我想我所要问的是,是否可以在控制台中获得条纹背景。

提前致谢!

2 个答案:

答案 0 :(得分:1)

使用Console.BackgroundColor并将其设置为枚举System.ConsoleColor中的任意值。您必须在每一行上更改它,以便您尝试执行此操作。不要使用.Clear,只需根据需要更改不同的块。

了解更多here

编辑:

这是代码示例。

using System;
using System.Collections.Generic;
using System.Linq;

namespace ConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            List<ConsoleColor> colors = Enum.GetValues(typeof(ConsoleColor)).Cast<ConsoleColor>().ToList();
            foreach (var back in colors)
            {
                Console.BackgroundColor = back;
                foreach (var fore in colors)
                {
                    Console.ForegroundColor = fore;
                    Console.Write("Test      ");
                }
            }
            Console.ReadLine();
        }
    }
}

答案 1 :(得分:1)

背景颜色仅应用于实际打印值的列。您可以通过空间填充整行来打印每列:

var width = Console.WindowWidth;
Console.BackgroundColor = System.ConsoleColor.DarkRed;
Console.WriteLine("{0, -" + width + "}", "TestRed");

这将使用深红色背景颜色打印整行。