+ 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.
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
+ 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
+ 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;
+ 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 :)
+ 1
True. Three is no other safe wat to do IT. Keep Coding
+ 1
I will need to keep coding if i plan to pass A level computer science :D