我正在尝试更改数组的背景颜色,特别是在这种情况下,网格[0,0]。我已经搜索了一段时间,似乎无法想出任何东西。这可能是一个非常简单的问题,或者我需要休息一下!
Console.BackgroundColor(grid[0,0]) = ConsoleColor.Cyan;
我正在尝试使背景颜色为青色。变量是一个字符串,包含一个空格。
提前干杯。
完整来源:
static void Main(string[] args)
{
Console.CursorSize = 100;
int row, col;
string[,] grid = new string[10, 10];
for (col = 0; col < 10; col++)
{
for (row = 0; row < 10; row++)
{
grid[col, row] = " ";
}
}
for (col = 0; col < 10; col++)
{
for (row = 0; row < 10; row++)
{
Console.Write(grid[col, row]);
}
Console.Write("\n");
}
Console.BackgroundColor(grid[0,0]) = ConsoleColor.Cyan;
Console.ReadKey();
}
答案 0 :(得分:1)
好的,首先你需要做的是grid
类型的颜色和字符串。
public class ColoredString
{
public ConsoleColor Color{get; set;}
public string Content {get; set;}
}
然后,当你设置颜色时,就这样做。
grid[0,0].Color = ConsoleColor.Cyan;
之后,您可以像这样打印
public static void PrintColor(ColoredString str)
{
var prevColor = Console.BackgroundColor;
Console.BackgroundColor = str.Color;
Console.Write(str.Content);
Console.BackgroundColor = prevColor;
}
这是一个SSCCE
public class Program
{
static void Main(string[] args)
{
var str = new ColoredString()
{
Color = ConsoleColor.Cyan,
Content = "abcdef",
};
PrintColor(str);
Console.ReadKey(false);
}
public static void PrintColor(ColoredString str)
{
var prevColor = Console.BackgroundColor;
Console.BackgroundColor = str.Color;
Console.Write(str.Content);
Console.BackgroundColor = prevColor;
}
}
public class ColoredString
{
public ConsoleColor Color { get; set; }
public string Content { get; set; }
}
答案 1 :(得分:0)
我很确定Console.BackgroundColor设置要打印的文本的颜色。因此,如果您想打印一个包含另一种颜色的字符串,请执行以下操作:
Console.Write("Hello word, the following text is cyan: ");
Console.BackgroundColor = ConsoleColor.Cyan;
Console.Write("Cyan text ");
Console.BackgroundColor = ConsoleColor.Black;
Console.WriteLine("(but this is not cyan)");