選択可能なメニューでリストの項目を使用する方法を考え出そうとしています。現在、このリストは、コンソールがデータベースからの情報で実行されるたびに入力されます。
私の目標は、リスト内のより多くの項目を表示して選択できるようにするために、書き直す必要のないコードを作成することです。
これが私が持っているものです。
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));
}
明らかに、リストの数が増えた場合に、選択可能な別のオプションを自動的に追加することはできませんでした。