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 : Down n Up キーを押したときに、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

下矢印キーの各ストロークで1ファイル下に移動するように聞こえる場合は、コードを次のように変更します。

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;
}

これにより、ユーザーが各キーを 2 回押す必要があったため、二重読み取りも修正されました。

于 2013-02-03T12:24:25.990 に答える