C
C#2d ago
Qvabbyte

✅ Need Help with .NET C# Console Project (visualizing) (SOLVED)

As you can see in the Image, I get following result, on first option and second I get normal result I want, but on third and so on as you can see that Background Color is even on next line and I don't really know why... I have my own Qprint Method in my dll
C#
public void Qprint(string msg, string TextColor, string BackgroundColor)
{
Console.ForegroundColor = (ConsoleColor)Enum.Parse(typeof(ConsoleColor), TextColor);
Console.BackgroundColor = (ConsoleColor)Enum.Parse(typeof(ConsoleColor), BackgroundColor);
Console.WriteLine(msg);
Console.ResetColor();
}
C#
public void Qprint(string msg, string TextColor, string BackgroundColor)
{
Console.ForegroundColor = (ConsoleColor)Enum.Parse(typeof(ConsoleColor), TextColor);
Console.BackgroundColor = (ConsoleColor)Enum.Parse(typeof(ConsoleColor), BackgroundColor);
Console.WriteLine(msg);
Console.ResetColor();
}
which I use to print the name of menu. (I'll send more code in other texts.)
No description
28 Replies
Qvabbyte
QvabbyteOP2d ago
Main Menu Code:
c#
public async Task MenuAsync()
{

try
{
visualizer.BreakLine(2);
visualizer.Qprint("\tEFCP MENU.\t", "Green","White");
visualizer.BreakLine(1);
var options = new List<string> { "1. User Management", "2. Games", "3. LeetCode Problems", "4. Exit" };
var optionsNums = options.Select(x => x.Split('.')[0]).ToList();
foreach (var i in options)
{
visualizer.Qprint(i, "White");
}
visualizer.QprintOnLine("\nSelect an option: ","White");
Console.ForegroundColor = ConsoleColor.Blue;
var input = Console.ReadLine();
visualizer.BreakLine();
Console.ResetColor();
bool isValid = false;
if (input != null)
isValid = optionsNums.Contains(input.ToString());
if (!isValid)
{
CMW.WriteErrorMessage("Invalid input. try again.");
await MenuAsync();
}
else
{
switch (input)
{
case "1":
await _userManagementService.UserManagementMenuAsync();
Console.ResetColor();
await MenuAsync();
break;
case "2":
await _gameService.GameMenuAsync();
Console.ResetColor();
await MenuAsync();
break;
case "3":
await _leetCodeProblemsService.LeetCodeProblemsMenuAsync();
Console.ResetColor();
await MenuAsync();
break;
case "4":
CMW.WriteSuccessMessage("Exiting EFCP. Goodbye :)");
Environment.Exit(0);
break;
default:
break;
}

}
}
catch (Exception e)
{
CMW.WriteErrorMessage(e.Message);
await MenuAsync();
}
}
c#
public async Task MenuAsync()
{

try
{
visualizer.BreakLine(2);
visualizer.Qprint("\tEFCP MENU.\t", "Green","White");
visualizer.BreakLine(1);
var options = new List<string> { "1. User Management", "2. Games", "3. LeetCode Problems", "4. Exit" };
var optionsNums = options.Select(x => x.Split('.')[0]).ToList();
foreach (var i in options)
{
visualizer.Qprint(i, "White");
}
visualizer.QprintOnLine("\nSelect an option: ","White");
Console.ForegroundColor = ConsoleColor.Blue;
var input = Console.ReadLine();
visualizer.BreakLine();
Console.ResetColor();
bool isValid = false;
if (input != null)
isValid = optionsNums.Contains(input.ToString());
if (!isValid)
{
CMW.WriteErrorMessage("Invalid input. try again.");
await MenuAsync();
}
else
{
switch (input)
{
case "1":
await _userManagementService.UserManagementMenuAsync();
Console.ResetColor();
await MenuAsync();
break;
case "2":
await _gameService.GameMenuAsync();
Console.ResetColor();
await MenuAsync();
break;
case "3":
await _leetCodeProblemsService.LeetCodeProblemsMenuAsync();
Console.ResetColor();
await MenuAsync();
break;
case "4":
CMW.WriteSuccessMessage("Exiting EFCP. Goodbye :)");
Environment.Exit(0);
break;
default:
break;
}

}
}
catch (Exception e)
{
CMW.WriteErrorMessage(e.Message);
await MenuAsync();
}
}
Game Menu Code:
c#
//Console Visualizers.
ConsoleMessagesWriter CMW = new ConsoleMessagesWriter();
ConsoleOutputVisualizer visualizer = new ConsoleOutputVisualizer();
public async Task GameMenuAsync()
{
try
{
//Game Menu
var options = new List<string> {"1. Wordle", "2. Exit" };
var optionsNums = options.Select(x => x.Split('.')[0]).ToList();
//Printing Menu

visualizer.BreakLine(2);
visualizer.Qprint("\tGAME MENU.\t", "Green", "White");
visualizer.BreakLine();

foreach (var i in options)
{
visualizer.Qprint(i, "Red");
}
//Getting Input
visualizer.QprintOnLine("\nSelect an option: ", "Red");
Console.ForegroundColor = ConsoleColor.Blue;
var input = Console.ReadLine();
visualizer.BreakLine();
Console.ResetColor();
//Checking Validation
bool isValid = false;
if (input != null)
isValid = optionsNums.Contains(input.ToString());
if (!isValid)
{
CMW.WriteErrorMessage("Invalid input. Please try again.");
await GameMenuAsync();
}
else
{
//If Valid
switch (input)
{
case "1":
WordleService wordleService = new WordleService();
wordleService.PlayWordle();
break;
case "2":
break;
default:
CMW.WriteErrorMessage("Invalid input. Please try again.");
await GameMenuAsync();
break;
}
}
}
catch (Exception e)
{
CMW.WriteErrorMessage($"An error occurred while displaying the game menu. Error Message: {e.Message}\nInner:{e.InnerException.Message} ");
}
}
c#
//Console Visualizers.
ConsoleMessagesWriter CMW = new ConsoleMessagesWriter();
ConsoleOutputVisualizer visualizer = new ConsoleOutputVisualizer();
public async Task GameMenuAsync()
{
try
{
//Game Menu
var options = new List<string> {"1. Wordle", "2. Exit" };
var optionsNums = options.Select(x => x.Split('.')[0]).ToList();
//Printing Menu

visualizer.BreakLine(2);
visualizer.Qprint("\tGAME MENU.\t", "Green", "White");
visualizer.BreakLine();

foreach (var i in options)
{
visualizer.Qprint(i, "Red");
}
//Getting Input
visualizer.QprintOnLine("\nSelect an option: ", "Red");
Console.ForegroundColor = ConsoleColor.Blue;
var input = Console.ReadLine();
visualizer.BreakLine();
Console.ResetColor();
//Checking Validation
bool isValid = false;
if (input != null)
isValid = optionsNums.Contains(input.ToString());
if (!isValid)
{
CMW.WriteErrorMessage("Invalid input. Please try again.");
await GameMenuAsync();
}
else
{
//If Valid
switch (input)
{
case "1":
WordleService wordleService = new WordleService();
wordleService.PlayWordle();
break;
case "2":
break;
default:
CMW.WriteErrorMessage("Invalid input. Please try again.");
await GameMenuAsync();
break;
}
}
}
catch (Exception e)
{
CMW.WriteErrorMessage($"An error occurred while displaying the game menu. Error Message: {e.Message}\nInner:{e.InnerException.Message} ");
}
}
Qprints:
c#


public void Qprint(string msg)
{
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine(msg);
Console.ResetColor();
}




public void Qprint(string msg, string TextColor)
{
Console.ForegroundColor = (ConsoleColor)Enum.Parse(typeof(ConsoleColor), TextColor);
Console.WriteLine(msg);
Console.ResetColor();
}



public void Qprint(string msg, string TextColor, string BackgroundColor)
{
Console.ForegroundColor = (ConsoleColor)Enum.Parse(typeof(ConsoleColor), TextColor);
Console.BackgroundColor = (ConsoleColor)Enum.Parse(typeof(ConsoleColor), BackgroundColor);
Console.WriteLine(msg);
Console.ResetColor();
}
c#


public void Qprint(string msg)
{
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine(msg);
Console.ResetColor();
}




public void Qprint(string msg, string TextColor)
{
Console.ForegroundColor = (ConsoleColor)Enum.Parse(typeof(ConsoleColor), TextColor);
Console.WriteLine(msg);
Console.ResetColor();
}



public void Qprint(string msg, string TextColor, string BackgroundColor)
{
Console.ForegroundColor = (ConsoleColor)Enum.Parse(typeof(ConsoleColor), TextColor);
Console.BackgroundColor = (ConsoleColor)Enum.Parse(typeof(ConsoleColor), BackgroundColor);
Console.WriteLine(msg);
Console.ResetColor();
}
so any Idea why this can happen? I can't even debug and understand where it gets messed up
ero
ero2d ago
i recommend using $spectre
MODiX
MODiX2d ago
Spectre.Console is a .NET library that allows for easy creation of console UIs, text formatting in the console, and command-line argument parsing. https://spectreconsole.net/
Spectre.Console - Welcome!
Spectre.Console is a .NET library that makes it easier to create beautiful console applications.
Qvabbyte
QvabbyteOP2d ago
Oh Thanks, but well I kinda want to have experience of making my own dll so that's why I choose to use mine
ero
ero2d ago
could it be that you're not awaiting MenuAsync in the switch? all this recursion makes me a bit sceptical about the stability of this it's unlikely you'll ever overflow the stack, but a loop or something would be less prone to risk and it's really not a negative to use another library; it'll teach you different things try another app with spectre in the future
Qvabbyte
QvabbyteOP2d ago
Okay, I know libraries don't hurt I just wanna experience making my own if you know what I mean well that didn't help thanks for noticing it tho If Anyone understands the problem solution please ping me
FusedQyou
FusedQyou2d ago
Try this
public void Qprint(string msg, string textColor, string backgroundColor)
{
Console.ForegroundColor = (ConsoleColor)Enum.Parse(typeof(ConsoleColor), textColor);
Console.BackgroundColor = (ConsoleColor)Enum.Parse(typeof(ConsoleColor), backgroundColor);

int width = Console.WindowWidth;
string paddedMsg = msg.PadRight(width - 1);
Console.WriteLine(paddedMsg);

Console.ResetColor();
}
public void Qprint(string msg, string textColor, string backgroundColor)
{
Console.ForegroundColor = (ConsoleColor)Enum.Parse(typeof(ConsoleColor), textColor);
Console.BackgroundColor = (ConsoleColor)Enum.Parse(typeof(ConsoleColor), backgroundColor);

int width = Console.WindowWidth;
string paddedMsg = msg.PadRight(width - 1);
Console.WriteLine(paddedMsg);

Console.ResetColor();
}
Alternatively, try this:
public void Qprint(string msg, string textColor, string backgroundColor)
{
Console.ForegroundColor = (ConsoleColor)Enum.Parse(typeof(ConsoleColor), textColor);
Console.BackgroundColor = (ConsoleColor)Enum.Parse(typeof(ConsoleColor), backgroundColor);

Console.Write(msg);
Console.ResetColor();
Console.WriteLine();
}
public void Qprint(string msg, string textColor, string backgroundColor)
{
Console.ForegroundColor = (ConsoleColor)Enum.Parse(typeof(ConsoleColor), textColor);
Console.BackgroundColor = (ConsoleColor)Enum.Parse(typeof(ConsoleColor), backgroundColor);

Console.Write(msg);
Console.ResetColor();
Console.WriteLine();
}
Qvabbyte
QvabbyteOP22h ago
I just woke up, I'll try None of them changed anything... I am just super confused now xd
FusedQyou
FusedQyou22h ago
Dunno then I assumed the console applied additional padding and overflowed the feature to the next line, but I guess it doesn't Is this something that ALWAYS happends, or conditionally?
Qvabbyte
QvabbyteOP22h ago
First time occuring this and it don't happen on first two menu appearence and after that it always shows up like that
Qvabbyte
QvabbyteOP22h ago
No description
Qvabbyte
QvabbyteOP22h ago
No description
Qvabbyte
QvabbyteOP22h ago
and then it goes like this
Qvabbyte
QvabbyteOP22h ago
Well I do have This two in code That I use so I already do this
No description
FusedQyou
FusedQyou22h ago
As an attempt you could try explicitl resetting everything back to default using both ResetColor and setting BackgroundColor in every single method before you write Just as a temporary test
Qvabbyte
QvabbyteOP22h ago
oh wait I just noticed something
Qvabbyte
QvabbyteOP22h ago
No description
Qvabbyte
QvabbyteOP22h ago
it went back to normal and then did that again that's weird lol
FusedQyou
FusedQyou22h ago
I personally never used the console for this so I'm out of options if it keeps happening
Qvabbyte
QvabbyteOP22h ago
How do I set BackgroundColor to default I did put resetColor in every method beginning but didn't work either
FusedQyou
FusedQyou22h ago
The default is black
Qvabbyte
QvabbyteOP22h ago
Okay so I cleaned the code and built and it now works normally, I'll switch back to my old code clean and build again to see if it works Looks like this works
FusedQyou
FusedQyou22h ago
So it does pad the result and it overflows
Qvabbyte
QvabbyteOP22h ago
No description
Qvabbyte
QvabbyteOP22h ago
yeah I think it was my fault that I didn't clean or rebuild that DLL file Thanks for helping!
ero
ero21h ago
$close
MODiX
MODiX21h ago
If you have no further questions, please use /close to mark the forum thread as answered
Unknown User
Unknown User15h ago
Message Not Public
Sign In & Join Server To View

Did you find this page helpful?