Print out a string at a specific coordinate, c# Console Application

10,380

Solution 1

The following code works perfectly for me:

Console.SetCursorPosition(20, 0);
Console.WriteLine("Hello");
Console.SetCursorPosition(20, 2);
Console.WriteLine("World");
Console.ReadLine();

You might want to explore WHY you want to use a custom class at all.

EDIT: Also, the values for x and y in the ConsoleObj class are never referred to. Just pointing that out.

Solution 2

Try changing this line:

Console.ForegroundColor = color;

to this:

Console.ForegroundColor = ConsoleColor.Gray;

You were inadvertently setting the foreground color to black.

Share:
10,380
user2057693
Author by

user2057693

Updated on June 04, 2022

Comments

  • user2057693
    user2057693 almost 2 years

    I want to create a program that prints out a string at any given coordinates. I have a main class and a class i created called ConsoleText. Here are the code for each class:

    class ConsoleText
    {
        public int x, y; // Coordinates
        public string Text = "Hello!";
        ConsoleColor color;
    
        public ConsoleText(int x, int y, string Text)
        {
            Console.ForegroundColor = color;
            Console.SetCursorPosition(x, y);
            Console.Write(Text);
            Console.ResetColor();
        }
    }
    

    and here is the program(main) class:

    class Program
    {
        static void Main(string[] args)
        {
            ConsoleText obj = new ConsoleText(19, 1, "Hello!");
            Console.Read();
        }
    }
    

    My problem now is that when I debug the program, I get the positions for the coordinate but the string dosen´t write out at that specific coordinate. Any ideas on what i might have done wrong?