Search code examples
c#consolegeneric-collections

How to move cursor for items in List in console C#?


I'd appreciate of someone could help to find a mistake. I printed items of my List in console and i need to move my cursor down, for each item to be highlighted, I've tried like this:

foreach (FileSystemInfo fsi in files)
         Console.WriteLine(fsi); //printing the "files" list

Console.SetCursorPosition(0, 0); //setting initial place of cursor
while (Console.ReadKey(true).Key != ConsoleKey.Escape)  //untill i press escape
{
    switch (Console.ReadKey(true).Key)
    {
        case ConsoleKey.DownArrow:   //for Down key
            foreach (FileSystemInfo fsi in files)
            {
                Console.BackgroundColor = ConsoleColor.Blue;
                Console.ForegroundColor = ConsoleColor.Green;

                // Here's the problem, the cursor goes to the end of the list, 
                // not moving through each item:
                Console.SetCursorPosition(0, files.IndexOf(fsi)); 

            }
            break;
    }
}
Console.ReadKey();

I'd be grateful for any help! Thanks in advance!


Solution

  • Sure it goes down the complete list due to the foreach within your case.

    I changed your filelist to a list of string for my example

    var files = new List<string>()
        {
            "File1", "File2", "File3", "File4",
        };
    
    foreach (var fsi in files)
        Console.WriteLine(fsi); //printing the "files" list
    
    // A counter to save the specific position
    int cnt = 0;
    Console.SetCursorPosition(0, cnt);//setting initial place of cursor
    
    // Save the pressed key so you dont need to press it twice
    ConsoleKey key;
    while ((key = Console.ReadKey(true).Key) != ConsoleKey.Escape)  //untill i press escape
    {
        switch (key)
        {
            case ConsoleKey.DownArrow:   //for Down key
                cnt++;
    
                if (cnt >= files.Count)
                {
                    cnt = files.Count - 1;
                }
    
                Console.BackgroundColor = ConsoleColor.Blue;
                Console.ForegroundColor = ConsoleColor.Green;
    
                // Here's the problem, the cursor goes to the end of the list, 
                // not moving through each item:
                Console.SetCursorPosition(0, cnt);
    
                break;
    
            case ConsoleKey.UpArrow:   //for Down key
                cnt--;
    
                if (cnt < 0)
                {
                    cnt = 0;
                }
    
                Console.BackgroundColor = ConsoleColor.Blue;
                Console.ForegroundColor = ConsoleColor.Green;
    
                // Here's the problem, the cursor goes to the end of the list, 
                // not moving through each item:
                Console.SetCursorPosition(0, cnt);
    
                break;
    
        }
    }
    Console.ReadKey();
    

    The color coding you are trying to do, does not yet work as you want, but I'm sure, you know how to fix it ;-)