如何在控制台C#中的列表中移动光标?

时间:2013-02-03 12:04:11

标签: c# console generic-collections

我很欣赏有人可以帮助找出错误。 我在控制台中打印了我的列表项目,我需要将光标向下移动,对于要突出显示的每个项目,我都尝试过这样:

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();

我很感激任何帮助! 提前谢谢!

3 个答案:

答案 0 :(得分:1)

这有效

    static void Main(string[] args)
    {
        string[] files = Directory.GetFiles(@"C:\");
        foreach (string file in files)
        {
            Console.WriteLine(file);
        }
        Console.SetCursorPosition(0, 0); //setting initial place of cursor
        int curPos = -1;

        if (files.Length > 0)
        {
            curPos = 0;
        }
        ConsoleKeyInfo keyinfo;
        while ((keyinfo = Console.ReadKey(true)).Key != ConsoleKey.Escape)  //untill i press escape
        {
            switch (keyinfo.Key)
            {
                case ConsoleKey.DownArrow:   //for Down key
                    curPos++;

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



                    break;
                case ConsoleKey.UpArrow:
                    curPos--;
                    Console.SetCursorPosition(0, curPos);
                    break;
            }
        }



    }

PS:在按下向上键时检查curPos绑定!休息一切正常!

答案 1 :(得分:0)

由于你的案件中的foreach,它肯定会落在完整列表中。

我将您的文件列表更改为我的示例

的字符串列表
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();

您正在尝试的颜色编码,但仍然无法正常工作,但我确定,您知道如何修复它; - )

答案 2 :(得分:0)

听起来您希望向下箭头键上的每个笔划向下移动一个文件,在这种情况下将代码更改为:

Console.SetCursorPosition(0, 0); //setting initial place of cursor
ConsoleKey key = Console.ReadKey(true).Key;
int index = 0;
while (key != ConsoleKey.Escape)  //untill i press escape
{
    switch (key)
    {
        case ConsoleKey.DownArrow:   //for Down key
            Console.BackgroundColor = ConsoleColor.Blue;
            Console.ForegroundColor = ConsoleColor.Green;
            index = (index + 1) % files.Count;
            Console.SetCursorPosition(0, index);
            break;
        }
        key = Console.ReadKey(true).Key;
}

这也修复了你所拥有的双重读数,这使用户不得不按两次键。