0

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

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

我将不胜感激任何帮助!提前致谢!

4

3 回答 3

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:当按下向下 n 向上键时,请检查是否绑定了 curPos!休息一切正常!

于 2013-02-03T12:23:30.247 回答
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();

您尝试使用的颜色编码尚未按您的意愿工作,但我敢肯定,您知道如何解决它 ;-)

于 2013-02-03T12:14:48.507 回答
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;
}

这也解决了您的双重阅读问题,导致用户必须按每个键两次。

于 2013-02-03T12:24:25.990 回答