Witam,
Mam problem. Stworzyłem taki kod:
Główna klasa:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Specjalne_Menu
{
class Program
{
static void Main(string[] args)
{
Console.CursorVisible = false;
Menu menu = new Menu();
menu.NaszeMenu();
while (true)
{
menu.WypiszTablice();
if (!menu.CzytanieKlawiszy(Console.ReadKey().Key))
break;
}
}
}
}
klasa Menu:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Specjalne_Menu
{
class Menu
{
int AktualnyElement = 0;
string[] menu;
public Menu()
{
NormalizowanieMenu();
}
#region Menu_Wypisywanie
public void NaszeMenu()
{
Console.Clear();
menu = new string[]
{ "W przod",
"W tyl",
"Pokaz opcje",
"O tworcy"
};
}
public void WypiszTablice()
{
Console.Clear();
for (int i = 0; i < menu.Length; i++)
{
if (i == AktualnyElement)
Console.WriteLine(">>" + menu[i]);
else
Console.WriteLine(menu[i]);
}
}
#endregion
#region CzytanieKlawiszy
public bool CzytanieKlawiszy(ConsoleKey klawisz)
{
switch (klawisz)
{
case ConsoleKey.UpArrow:
AktualnyElement--;
break;
case ConsoleKey.DownArrow:
AktualnyElement++;
break;
case ConsoleKey.Escape:
return false;
default:
return true;
}
if (AktualnyElement < 0)
AktualnyElement = menu.Length - 1;
if (AktualnyElement >= menu.Length)
AktualnyElement = 0;
return true;
}
#endregion
public void NormalizowanieMenu()
{
int max = SzukanieNajdluzszegoElementu(menu); //<---- tutaj problem
for (int i = 0; menu.Length > i; i++)
menu[i] = menu[i].PadRight(max);
}
public int SzukanieNajdluzszegoElementu(string[] tabka)
{
int max = 0;
for (int i = 0; i < tabka.Length; i++)
{
if (tabka[i].Length > max)
max = tabka[i].Length;
}
return max;
}
}
}
Kiedyś miałem podobny problem (Jak przypisać tablicę w NaszeMenu() do pola w klasie), ale udało się go rozwiązać. Teraz miałem nadzieję, że jak wcześniej wywołam metodę NaszeMenu() to publiczne pola tablicy będzie zapełnione jej danymi. Być może się mylę, proszę o pomoc.
Z góry dzięki.