Question

I'm Junior programmer I Wrote this Code in Console application

static void Main(string[] args)
{
    Console.BackgroundColor = ConsoleColor.Red;
    Console.WriteLine("backgroundcolor is red");
    Console.ForegroundColor = ConsoleColor.Green;
    Console.Write("ForegroundColor is Green");
    Console.ReadKey();
}

But I want to Write one console write line. So background color is red just with background color in console and Foreground Color is Green write in console application just with Foreground Color at a same time that each sentence effected with its class in one line.

Était-ce utile?

La solution

What about this...

static void Main(string[] args)
{
 var originalColor = Console.BackgroundColor;
 Console.BackgroundColor = ConsoleColor.Red;
 Console.Write("The background color is red. ");
 Console.BackgroundColor = originalColor;
 Console.ForegroundColor = ConsoleColor.Green;
 Console.Write("The foreground color is green");
 Console.ReadKey();
}

Autres conseils

I think is what you want. Correct me if I am wrong.

static void Main(string[] args)
    {
        var originalColor = Console.BackgroundColor;
        Console.BackgroundColor = ConsoleColor.Red;
        Console.ForegroundColor = ConsoleColor.Green;
        Console.WriteLine("The BackgroundColor is " + Console.BackgroundColor.ToString() + " and the ForegroundColor is " + Console.ForegroundColor.ToString());
        Console.ReadKey();
    }

I think this will fix your problem.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top