+ 1

(C#) What is the best way to set the foreground colour in a console app to a user input.

Is there a way to set the ForegroundColor equal to a colour that the user inputs. so essentially ForegroundColor = userInputedColour.

2nd Oct 2019, 8:04 PM
Charlie
Charlie - avatar
6 Réponses
+ 1
It is difficult to link user input to Console.ForegroundColors. Users always have unpredictable input and you need to be prepared for that. This is how I should do it. It is not very efficient, but it is simple and it works. https://code.sololearn.com/c1fbPWBhfxch
2nd Oct 2019, 8:44 PM
sneeze
sneeze - avatar
+ 2
I am not sure why you want to do this. But fore educational purposes it is great. The easiest way will be 1) getting color user input as string 2) use a case statement to link to the according Console.ForegroundColor 3) set the color https://www.geeksforgeeks.org/c-sharp-how-to-change-foreground-color-of-text-in-console/ https://stackoverflow.com/questions/48997560/multiple-colors-in-one-line-in-console-application
2nd Oct 2019, 8:21 PM
sneeze
sneeze - avatar
+ 2
I have basically got my code up to where it is on geeksforgeeks already, it's now just working out how to do this, but in a way that works: string colour; Console.WriteLine("Input a color"); colour = Console.ReadLine(); Console.ForegroundColor = colour;
2nd Oct 2019, 8:33 PM
Charlie
Charlie - avatar
+ 1
I thought I might have to do something like that with a switch or if statement, was just hoping i didnt have to, will be long if i did all colours for foreground. But i prepared for unpredictable input by getting an array of all the foreground colours and logging them to the console and telling the user to pick one from the list. Thanks for your help :)
2nd Oct 2019, 8:59 PM
Charlie
Charlie - avatar
+ 1
True. Three is no other safe wat to do IT. Keep Coding
2nd Oct 2019, 9:01 PM
sneeze
sneeze - avatar
+ 1
I will need to keep coding if i plan to pass A level computer science :D
2nd Oct 2019, 9:02 PM
Charlie
Charlie - avatar