Console Text Foreground Color Changing in C#

  1. using System;  
  2. namespace console_color_on_demand  
  3. {  
  4.     class Console_Color  
  5.     {  
  6.         Type type = typeof (ConsoleColor);  
  7.         string sampleText;  
  8.         string color;  
  9.         string defaultColor = "White";  
  10.         public Console_Color()  
  11.         {  
  12.             sampleText = "Sample text here!";  
  13.             color = "White";  
  14.         }  
  15.         public void GetData()  
  16.         {  
  17.             Console.WriteLine("Enter a string!");  
  18.             sampleText = Console.ReadLine();  
  19.             Console.WriteLine("Choose a color:\n1.\tRed\n2.\tYellow\n3.\tDarkBlue\n4.\tDarkGreen\n5.\tDarkCyan\n6.\tDarkRed \n7.\tDarkMagenta\n8.\tDarkYellow\n9.\tGray\n10.\tDarkGray\n11.\tGreen\n12.\tCyan\n13.\tMagenta                                    \n14.\tWhite (default)\n");  
  20.             int choice = Convert.ToInt32(Console.ReadLine());  
  21.             GetColorTextData(choice);  
  22.             Console.WriteLine("Do you want to make this color default?\n1.\tYes\n2.\tNo\n");  
  23.             int ans = Int32.Parse(Console.ReadLine());  
  24.             if (ans == 1)  
  25.             {  
  26.                 defaultColor = color;  
  27.             }  
  28.             DisplayTextInColor();  
  29.             defaultTextColor();  
  30.             Console.WriteLine("Press Ctrl+C to exit");  
  31.             Console.ReadKey();  
  32.             Console.Clear();  
  33.         }  
  34.         void defaultTextColor()  
  35.         {  
  36.             Console.ForegroundColor = (ConsoleColor) Enum.Parse(type, defaultColor);  
  37.         }  
  38.         void DisplayTextInColor()  
  39.         {  
  40.             Console.ForegroundColor = (ConsoleColor) Enum.Parse(type, color);  
  41.             Console.WriteLine(sampleText);  
  42.             Console.ResetColor();  
  43.         }  
  44.         void GetColorTextData(int choices)  
  45.         {  
  46.             switch (choices)  
  47.             {  
  48.             case 1:  
  49.                 color = "Red";  
  50.                 break;  
  51.             case 2:  
  52.                 color = "Yellow";  
  53.                 break;  
  54.             case 3:  
  55.                 color = "DarkBlue";  
  56.                 break;  
  57.             case 4:  
  58.                 color = "DarkGreen";  
  59.                 break;  
  60.             case 5:  
  61.                 color = "DarkCyan";  
  62.                 break;  
  63.             case 6:  
  64.                 color = "DarkRed";  
  65.                 break;  
  66.             case 7:  
  67.                 color = "DarkMagenta";  
  68.                 break;  
  69.             case 8:  
  70.                 color = "DarkYellow";  
  71.                 break;  
  72.             case 9:  
  73.                 color = "Gray";  
  74.                 break;  
  75.             case 10:  
  76.                 color = "DarkGray";  
  77.                 break;  
  78.             case 11:  
  79.                 color = "Green";  
  80.                 break;  
  81.             case 12:  
  82.                 color = "Cyan";  
  83.                 break;  
  84.             case 13:  
  85.                 color = "Red";  
  86.                 break;  
  87.             case 14:  
  88.                 color = "Magenta";  
  89.                 break;  
  90.             default:  
  91.                 color = "White";  
  92.                 break;  
  93.             }  
  94.         }  
  95.     }  
  96.     class Program  
  97.     {  
  98.         static void Main(string[] args)  
  99.         {  
  100.             var c1 = new Console_Color();  
  101.             while (true)  
  102.             {  
  103.                 c1.GetData();  
  104.             }  
  105.         }  
  106.     }  
  107. }