0

選択可能なメニューでリストの項目を使用する方法を考え出そうとしています。現在、このリストは、コンソールがデータベースからの情報で実行されるたびに入力されます。

私の目標は、リスト内のより多くの項目を表示して選択できるようにするために、書き直す必要のないコードを作成することです。

これが私が持っているものです。

    public void Menu()
    {
        var list = new Select();
        var cki = new ConsoleKeyInfo();
        var menuStationaryItems = menuStationaryItems();
        var menuSelectableItems = menuSelectableItems();

        short curMenuItem = 0, menuSelected;

        const string listborder = "-*************************-";
        const string noitems = "Worlds are currently unavailable.";

        list.Select(); // This fills the list with database entries

        do
        {
            Console.Clear();

            if (Program.list.Count == 0)
            {
                Console.Clear();
                Console.WriteLine();
                Console.WriteLine("{0," + ((Console.WindowWidth / 2) + (listborder.Length / 2)) + "}", listborder);
                Console.WriteLine();
                Console.WriteLine("{0," + ((Console.WindowWidth / 2) + (noworlds.Length / 2)) + "}", noitems);
                Console.WriteLine();
                Console.WriteLine("{0," + ((Console.WindowWidth / 2) + (listborder.Length / 2)) + "}", listborder);
                Console.WriteLine();
                Thread.Sleep(4000);
            }
            else
            {
                Console.Clear();
                Console.WriteLine("{0," + ((Console.WindowWidth / 2) + (listborder.Length / 2)) + "}", listborder);
                Console.WriteLine();

                foreach (var item in Program.list)
                {

                    Console.WriteLine("{0," + ((Console.WindowWidth / 2) + (menuStationaryItems[0].Length / 2)) + "}{1}", menuStationaryItems[0], item.name);
                    Console.WriteLine("{0," + ((Console.WindowWidth / 2) + (menuStationaryItems[1].Length / 2)) + "}{1}", menuStationaryItems[1], item.description);
                    Console.WriteLine();

                }


                Console.WriteLine("{0," + ((Console.WindowWidth / 2) + (listborder.Length / 2)) + "}", listborder);
                Console.WriteLine();
            }

            // The loop that goes through all of the menu items.
            for (menuSelected = 0; menuSelected < menuSelectableItems.Length; menuSelected++)
            {
                // If the current item number is our variable "selected", tab out this option.
                // You could easily change it to simply change the color of the text.
                if (curMenuItem == menuSelected)
                {
                    Console.WriteLine("{0," + ((Console.WindowWidth / 2) - (menuSelectableItems[menuSelected].Length / 2)) + "}" + "{1}" + "{2}",
                        menuStationaryItems[1], menuSelectableItems[menuSelected], menuStationaryItems[2]);
                }
                // Just write the current option out if the current item is not our variable "selected".
                else
                {
                    Console.WriteLine("{0," + ((Console.WindowWidth / 2) + (menuSelectableItems[menuSelected].Length / 2)) + "}", menuSelectableItems[menuSelected]);
                }
            }

            // Waits until the user presses a key, and puts it into our object key.
            cki = Console.ReadKey(true);

            // If curItem goes below 0 or above the maximum menu item, it just loops around to the other end.
            if (cki.Key == ConsoleKey.DownArrow)
            {
                curMenuItem++;
                if (curMenuItem > menuSelectableItems.Length - 1) curMenuItem = 0;
            }
            else if (cki.Key == ConsoleKey.UpArrow)
            {
                curMenuItem--;
                if (curMenuItem < 0) curMenuItem = Convert.ToInt16(menuSelectableItems.Length - 1);
            }

            if ((cki.Key == ConsoleKey.Enter && curMenuItem == 0))
            {
                // If item one is selected run the following code
            }

            if ((cki.Key == ConsoleKey.Enter && curMenuItem == 1))
            {
                // If item two is selected run the following code
            }

            if ((cki.Key == ConsoleKey.Enter && curMenuItem == 2))
            {
                // If item three is selected run the following code
            }

            if ((cki.Key == ConsoleKey.Enter && curMenuItem == 3))
            {
                // If item four is selected run the following code
            }

            // Loop around until the user presses the enter button or the space bar.
        } while ((cki.Key != ConsoleKey.Enter || curWorldMenuItem != 4) && (cki.Key != ConsoleKey.Spacebar || curWorldMenuItem != 4));

    }

明らかに、リストの数が増えた場合に、選択可能な別のオプションを自動的に追加することはできませんでした。

4

1 に答える 1

0

それで、もう少し考えた後、私は何をする必要があるかを理解することができました. 以下は私のコードです。

public void Menu()
    {
        var list = new CreateList();
        var menuStationaryItems = menuStationaryItems();
        short curSelectMenuItem = 0, selectMenuSelected; 
        var cki = new ConsoleKeyInfo();

        const string listborder = "-*************************-";
        const string nolistitems = "You don't have anything in the list.";
        const string selectleftarrow = "-->";
        const string selectrightarrow = "<--";

        list.ListData();

        do
        {

            if (Program.list.Count == 0)
            {
                Console.Clear();
                Console.WriteLine();
                Console.WriteLine("{0," + ((Console.WindowWidth / 2) + (listborder.Length / 2)) + "}", listborder);
                Console.WriteLine();
                Console.WriteLine("{0," + ((Console.WindowWidth / 2) + (nolistitems.Length / 2)) + "}", nolistitems);
                Console.WriteLine();
                Console.WriteLine("{0," + ((Console.WindowWidth / 2) + (listborder.Length / 2)) + "}", listborder);
                Console.WriteLine();
                Thread.Sleep(4000);
            }
            else
            {
                Console.Clear();
                Console.WriteLine("{0," + ((Console.WindowWidth / 2) + (listborder.Length / 2)) + "}", listborder);
                Console.WriteLine();

                foreach (var itemin Program.list)
                {

                    Console.WriteLine("{0," + ((Console.WindowWidth / 2) + (menuStationaryItems[0].Length / 2)) + "}{1}", menuStationaryItems[0], item.name);
                    Console.WriteLine();

                }

                Console.WriteLine("{0," + ((Console.WindowWidth / 2) + (listborder.Length / 2)) + "}", listborder);
                Console.WriteLine();

                for (selectMenuSelected = 0; selectMenuSelected < Program.list.Count; selectMenuSelected++)
                {
                    if (curSelectMenuItem == selectMenuSelected)
                    {
                        Console.WriteLine("{0," + (Console.WindowWidth / 2) + "}{1}{2}"
                            , selectleftarrow, Program.list[selectMenuSelected].name, selectrightarrow);
                    }
                    else
                    {
                        Console.WriteLine("{0," + (Console.WindowWidth / 2) + "}", Program.list[selectMenuSelected].name);
                    }
                }

                cki = Console.ReadKey(true);

                if (cki.Key == ConsoleKey.DownArrow)
                {
                    curSelectMenuItem++;
                    if (curSelectMenuItem > Program.list.Count - 1) curSelectMenuItem = 0;
                }
                else if (cki.Key == ConsoleKey.UpArrow)
                {
                    curSelectMenuItem--;
                    if (curSelectMenuItem < 0) curSelectMenuItem = Convert.ToInt16(Program.list.Count - 1);
                }

                for (var i = 0; i < Program.list.Count; i++)
                {
                    if ((cki.Key == ConsoleKey.Enter && curSelectMenuItem == i))
                    {
                        variable = Program.list[curSelectMenuItem].name;
                    }
                }
            }
        } while (cki.Key != ConsoleKey.Enter);
    }
于 2012-12-13T09:55:39.937 に答える