0

I am trying to change the colour of my Console.WriteLine() text, but it's not working as expected. My code (in my WPF page constructor, but I've tried the same code elsewhere):

Console.BackgroundColor = ConsoleColor.Magenta;
Console.ForegroundColor = ConsoleColor.Yellow;
Console.Write("sdfsgfsd");
Console.WriteLine("sdgdsfg");

My text displays in the Output window, but in the usual grey on black console colours.

Is my code incorrect (I've seen several examples online that use the same method)? Or perhaps there is something in my Visual Studio setup that is incompatible? I've reset the console colour settings to the defaults with no effect.

This is VS Community 2019 with all available updates, using C# in a WPF project.

Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
pumpkinszwan
  • 1,312
  • 11
  • 21
  • 1
    "*WPF page constructor,*" why are you trying to change the Console color in WPF ? there is no console there? – TheGeneral Jul 03 '20 at 03:18
  • 2
    by _"Output window"_ you mean Visual Studio's one? – vasily.sib Jul 03 '20 at 03:22
  • Yes, I'm trying to change the text in the output window. Is this not possible? Console and output window seem to sometimes refer to the same thing, so it's confusing. – pumpkinszwan Jul 03 '20 at 03:36
  • 1
    @pumpkinszwan why do you want to change it there? It make no sense to change it from code (because when you finish your program and deploy it somewhere - there will be no Visual Studio). However, I think it's possible with some kind of Visual Studio Themes :\ – vasily.sib Jul 03 '20 at 03:43
  • You can refer this : https://stackoverflow.com/questions/7505623/colors-in-javascript-console#:~:text=Use%20Markdown%20for%20log%20messages.&text=The%20CSS%20format%20specifier%20allows,apply%20as%20the%20second%20parameter. – Rohit Tagadiya Jul 03 '20 at 03:52
  • @vsily.sib I just want the colour for convenience when testing and debugging so certain logs stand out from others. I'm building tools that I never deploy - I run directly from VS. I use coloured log messages when I develop in Unity, and I thought the same would be useful in Visual Studio. – pumpkinszwan Jul 03 '20 at 04:04
  • 2
    Your code is indeed correct, but as previous comments point out Output window in VS does not support that. You have some options as covered in duplicate I linked. Consider also searching for VS plugins as this seem to be somewhat popular idea. – Alexei Levenkov Jul 03 '20 at 04:27

1 Answers1

0

Console.BackgroundColor = ConsoleColor.Magenta; changes the background color Console.ForegroundColor = ConsoleColor.Yellow; changes the foreground color

if you want to change the text of a string you will need to use Console.Write in this format: Console.ForegroundColor = Console.Color.Yellow; Console.Write("Hello World!"); and you would use Console.BackgroundColor for the background color as one may expect. You unfortunately cannot do anything otherwise. Alternatively I have seen nuget libraries that do this more seemlessly ( and allow for the full RGB color space ) such as SadConsole. However you should know there is no console in WPF this would be of no use. In that case, I suggest you use the ForeColor property as shown: TextBox.ForeColor = Color.Red; And for specific words, you can use http://msdn.microsoft.com/en-us/library/system.windows.forms.richtextbox.aspx

I should also add that if you wanted to make individual words different respective colors you could just do:

Console.ForegroundColor = Console.Color.Yellow; Console.Write("Hello!");
Console.ForegroundColor = Console.Color.Red; Console.Write("World!");

  • 2
    While this answer provides a lot of useful and correct information it does not actually answer the question OP had about colorizing text in VS' Output window. When answering make sure to actually answer the question as asked before providing extra information. (and just for fun - indeed one can have console window in WPF if they really want to - https://stackoverflow.com/questions/7558739/how-can-i-add-a-console-output-to-a-windows-wpf-application-c-sharp, questionable idea and likely will require too much effort to get `Console.XXX` to work but plausible) – Alexei Levenkov Jul 03 '20 at 04:31