none
Creating a method that can temporarily change the color of the console text and then restore it after an operation happened inside of it. RRS feed

  • Question

  • The method im trying to create is basically changing text color of the console do some operation that isn't specified inside of the method then change color of the console text to something else.
    The code is:
    public static void ColorFancy(ConsoleColor c1, ConsoleColor c2, Action action)
    {
       Console.ForegroundColor = c1;
       action.Invoke();
       Console.ForegroundColor = c2;
    }
    
    
    //////////////inside of the main
    
    
    ColorFancy(ConsoleColor.Black, ConsoleColor.Blue, () =>
          {
             // lots of things here
          });
    Even though it works fine with things like WriteLine, its not working well with variables from outside the method, for example changing a string value that i created in main.
    Is there a way around this?
    Tuesday, January 8, 2019 12:15 AM

Answers

  • Your code example should work fine. Everything inside the action block will execute and any Console calls will write using the given foreground color. Are you seeing different behavior? If so then please post the full code that isn't behaving correctly. 

    Note also that we generally don't change the foreground to 2 different colors. Generally we change to a specific color and then revert back. This is common for things like writing errors or warnings in different colors. 

    //Didn't try compiling this, pulling from memory
    //General purpose method
    public static void WriteText ( ConsoleColor color, Action action )
    {
       var originalColor = Console.ForegroundColor;
       try
       {
          action();
       } finally
       {
          Console.ForegroundColor = originalColor;
       };
    }
    
    public static void WriteError ( Action action )
    {
       WriteText(ConsoleColor.Red, action);
    }
    
    public static void WriteWarning ( Action action )
    {
       WriteText(ConsoleColor.Yellow, action);
    }


    Michael Taylor http://www.michaeltaylorp3.net

    • Marked as answer by xanrer Wednesday, January 9, 2019 1:42 AM
    Tuesday, January 8, 2019 3:14 PM
    Moderator

All replies

  • Hi xanrer,

    Thank you for posting here.

    >>for example changing a string value that i created in main.
    Is there a way around this?

    For your question, I do not understand what does this mean. The code below works for all the value output in the console.

     Console.ForegroundColor = ConsoleColor.DarkRed;

    How do you want to change the value? If it is possible, could you provide a code sample which could reproduce your situation for me to test?

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, January 8, 2019 7:10 AM
    Moderator
  • If you do not want to change the strings, then use new local variables inside your action:

    string a = . . .

    ColorFancy( ConsoleColor.Black, ConsoleColor.Blue, ( ) =>

       {

          string b = a;

          // use b instead of a

          . . .

          b = “other text”;

          . . .

       });

    • Edited by Viorel_MVP Tuesday, January 8, 2019 9:45 AM
    Tuesday, January 8, 2019 9:43 AM
  • Your code example should work fine. Everything inside the action block will execute and any Console calls will write using the given foreground color. Are you seeing different behavior? If so then please post the full code that isn't behaving correctly. 

    Note also that we generally don't change the foreground to 2 different colors. Generally we change to a specific color and then revert back. This is common for things like writing errors or warnings in different colors. 

    //Didn't try compiling this, pulling from memory
    //General purpose method
    public static void WriteText ( ConsoleColor color, Action action )
    {
       var originalColor = Console.ForegroundColor;
       try
       {
          action();
       } finally
       {
          Console.ForegroundColor = originalColor;
       };
    }
    
    public static void WriteError ( Action action )
    {
       WriteText(ConsoleColor.Red, action);
    }
    
    public static void WriteWarning ( Action action )
    {
       WriteText(ConsoleColor.Yellow, action);
    }


    Michael Taylor http://www.michaeltaylorp3.net

    • Marked as answer by xanrer Wednesday, January 9, 2019 1:42 AM
    Tuesday, January 8, 2019 3:14 PM
    Moderator
  • The problem.. fixed itself somehow, it wasn't allow me to do:

    ColorFancy(ConsoleColor.White, ConsoleColor.Blue, () => typed = Console.ReadLine().ToLower());

    But now its just working.. i don't know what changed that.

    But thanks to everyone regardless.

    Tuesday, January 8, 2019 8:05 PM