可扩展的&使用foreach循环中列表中的项目的可选菜单

时间:2012-12-12 03:58:11

标签: c# console

我正试图想出一种方法来使用可选菜单中列表中的项目。 现在,每次使用数据库中的信息运行控制台时,此列表都会填充。

我的目标是让代码不需要重写,以允许显示和选择列表中的更多项目。

这是我拥有的;

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

    }

显然,如果列表数量增加,我还无法自动添加另一个可选选项。

1 个答案:

答案 0 :(得分: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);
    }