Question

Suppose i have the following interface on a C# Console Application:

enter image description here

And i would like to write something the user tells me to write (his name for example), and it would leave the interface like this:

enter image description here

Is there a way to write inside of that? maybe thinking about the location of the cursor could work? i've tried to do it but i dont know how to do it fine, so it doesnt work?

To generate thos 3 boxes of '' I would use a "for" from the beginning of the console till the end, and i would place the '' as i need.

Was it helpful?

Solution

I think moving the cursor each time is not really a good idea. Your program will contain a lot of logic to place the cursor at precise positions, you'll have a lot of magic numbers, and so on.

An alternative would be to simply draw your boxes, and then after each input Clear() the console and redraw them. So your main loop would ask for a name, store it and redraw your boxes with the new input. To draw the box, simply loop on all your names (even empty ones) and Pad them to have exactly the same length on each row.

Here is a complete example so you can see and understand the behavior (I've also hard-capped the input to 8 characters and 5 names to prevent edge cases, you can edit that quickly with the constants. And sorry for the messy StoreUserAnswer method). As you'll see, clearing the console do not flicker the screen.

namespace ConsoleApplication1
{
    class Program
    {
        private const int MAX_NAME_LENGTH = 8;
        private const int MAX_NAMES = 5;

        private static string[] names = new string[MAX_NAMES];
        private static int currentIndex;

        static void Main(string[] args)
        {
            DrawBox();

            while (true)
            {
                AskForAName();
                StoreUserAnswer();
                DrawBox();
            }
        }

        private static void AskForAName()
        {
            Console.WriteLine("Enter a name:");
        }

        private static void StoreUserAnswer()
        {
            string name = Console.ReadLine() ?? string.Empty;
            if (name.Length > MAX_NAME_LENGTH)
            {
                name = name.Substring(0, MAX_NAME_LENGTH);
            }

            names[currentIndex] = name;
            currentIndex++;
            if (currentIndex > MAX_NAMES - 1)
            {
                currentIndex = 0;
            }
        }

        private static void DrawBox()
        {
            Console.Clear();
            DrawDelimiter();
            DrawNames();
            DrawDelimiter();
        }

        private static void DrawDelimiter()
        {
            Console.WriteLine("*{0}*", new String('*', MAX_NAME_LENGTH));
        }

        private static void DrawNames()
        {
            for (int i = 0; i < MAX_NAMES; i++)
            {
                DrawName(names[i] ?? string.Empty);
            }
        }

        private static void DrawName(string name)
        {
            Console.WriteLine("*{0}*", name.PadRight(MAX_NAME_LENGTH, ' '));
        }
    }
}

OTHER TIPS

you want Console.SetCursorPosition

something like:

Console.SetCursorPosition(10,0);
Console.Write("Carl");

You can do a decent amount with the Console. Look through the documentation

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top