0

I'm currently trying to change the background color of an array, specifically in this case, grid[0, 0]. I've searched around for a while and can't seem to come up with anything. It's probably quite a simple problem, or maybe I need a break!

Console.BackgroundColor(grid[0,0]) = ConsoleColor.Cyan;

I'm trying to make the background colour Cyan. The variable is a string and contains a space.

Cheers in advance.

FULL SOURCE:

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();
    }
  • What is `grid`? `Console.BackgroundColor` is a property and you're trying to use it like a method. Maybe you just want `Console.BackgroundColor = ConsoleColor.Cyan;`, but I don't know how `grid` applies here. – juharr Mar 09 '15 at 19:37
  • What are you trying to set? The background color of the text in an array? – Patrick Hofman Mar 09 '15 at 19:37
  • "the background color of an array" is not a phrase that makes sense (what colour do you want that database?).. What exactly are you trying to do? – Blorgbeard Mar 09 '15 at 19:37
  • [_`Console.BackgroundColor` affects only output that is written to individual character cells after the background color is changed_.](https://msdn.microsoft.com/en-us/library/system.console.backgroundcolor%28v=vs.110%29.aspx) – cbr Mar 09 '15 at 19:38
  • I believe he wants to change the color of individual cells in the console buffer. – cbr Mar 09 '15 at 19:38
  • Then it is a dup of this one: http://stackoverflow.com/questions/11150332/how-to-change-foreground-color-of-each-letter-in-a-string-in-c-sharp-console – Patrick Hofman Mar 09 '15 at 19:39
  • I'm trying to set the color of individual cells yes. Grid is the name of my 2d array –  Mar 09 '15 at 19:39
  • @Viroe How are you outputting your `grid` onto the console? – cbr Mar 09 '15 at 19:41
  • @GrawCube the Grid is being displayed as a for loop, edited the code to display my source code for you. –  Mar 09 '15 at 19:46

2 Answers2

1

Okay, first thing you need to do is to make grid of a type that can contain both a color and a string.

public class ColoredString
{
    public ConsoleColor Color{get; set;}
    public string Content {get; set;}
}

and then, when you set your color, do it like this.

grid[0,0].Color = ConsoleColor.Cyan;

after that, you can print in color like this

public static void PrintColor(ColoredString str)
{
    var prevColor = Console.BackgroundColor;
    Console.BackgroundColor = str.Color;
    Console.Write(str.Content);
    Console.BackgroundColor = prevColor;
}

Here's a 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; }
}
  • Thanks Sam, would this method work for the source code edited in? I would like [0,0] to start as a player color e.g red. and as the player goes along, each cell will change accordingly –  Mar 09 '15 at 19:52
  • @viroe You're going to have to reprint the grid every time you want to see the updated status. That's just the nature of the console app. It more or less prints text and doesn't often delete text. [It is possible to overwrite text on the console](http://stackoverflow.com/questions/5195692/is-there-a-way-to-delete-a-character-that-has-just-been-written-using-console-wr), but if you want to display a changing area on your screen, a console app probably isn't what you should be going for. – Sam I am says Reinstate Monica Mar 09 '15 at 19:59
  • @Viroe You should do a google search for `XNA`. It's a game making framework that uses C# and visual studio, and I seem to remember there being some pretty simple tutorials for using it. – Sam I am says Reinstate Monica Mar 09 '15 at 20:00
  • @SamIam Sorry to be pain Sam, would you mind providing me with an example of how I'm able to turn grid[0, 0]. I still can't get my head around the idea. I think with an example I'll be able to understand it better. Cheers –  Mar 09 '15 at 20:03
  • @Viroe `ColoredString[,] grid = new ColoredString[10, 10];` – Sam I am says Reinstate Monica Mar 09 '15 at 20:11
  • @Viroe `grid[col, row].Content = " ";` – Sam I am says Reinstate Monica Mar 09 '15 at 20:12
0

I am pretty sure that Console.BackgroundColor sets the color of the text to be printed. So, if you want to print a string with one word of another color, you would do:

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)");
Henrik Karlsson
  • 5,559
  • 4
  • 25
  • 42