C#C
C#3y ago
Rémi

❔ Interactive both in y and x axis, in cmd

Hello :), I am trying to do an interactive menu in a command line application that use both the x and y axis, you navigate using keyboard arrows. I did a well working one that is only using the x axis, but with both this is way more complicated. I wrote a code that kind of works but for any reason the Console.Clear is not doing what i was expecting :
public static void Editing()
    {
        int x = 0;
        int y = 0;
        List<Option> x_options = new List<Option>();
        List<string> y_options = GetPrompt();
        List<string> special = new List<string>() { "%path", "%user", "%os", "%workingset", "%time", "%date", "%newline" };
        List<string> colors = new List<string>() { "%fore:", "%back:", "%style:" };
        while (true)
        {
            if (special.Contains(y_options[y]) || colors.Contains(y_options[y]))
            {
                x_options.Add(new Option(CustomCMD.GetValue("tips"), () => { }));
            }
            x_options.Add(new Option(CustomCMD.GetValue("edit"), () => { }));
            x_options.Add(new Option(CustomCMD.GetValue("add_right"), () => { }));
            x_options.Add(new Option(CustomCMD.GetValue("add_left"), () => { }));
            x_options.Add(new Option(CustomCMD.GetValue("delete"), () => { }));

            WriteComplexMenu(x_options, y_options, x_options[x], y_options[y]);
            ConsoleKeyInfo key = Console.ReadKey();
            if (key.Key == ConsoleKey.RightArrow)
            {
                y--;
                if (y < 0) y = y_options.Count - 1;
            }
            else if (key.Key == ConsoleKey.LeftArrow)
            {
                y++;
                if (y >= y_options.Count) y = 0;
            }
            else if (key.Key == ConsoleKey.UpArrow)
            {
                x--;
                if (x < 0) x = x_options.Count - 1;
            }
            else if (key.Key == ConsoleKey.DownArrow)
            {
                x++;
                if (x >= x_options.Count) x = 0;
            }
            else if (key.Key == ConsoleKey.Enter)
            {
            }
        }
    }

private static void WriteComplexMenu(List<Option> x_options, List<string> y_options, Option x_selected, string y_selected, string title="", string end="")
    {
        Console.Clear();
        if (title != "") Console.WriteLine(title);
        foreach (Option option in x_options)
        {
            if (option == x_selected)
            {
                Console.Write(" > ");
            }
            else
            {
                Console.Write("   ");
            }
            Console.WriteLine(option.Name);
        }
        Console.WriteLine(end);
        int tab = 0;
        bool found = false;
        foreach (string option in y_options)
        {
            if (option == y_selected)
            {
                found = true;
                tab += Math.Abs(option.Length / 2);
            }
            else if (!found)
            {
                tab += option.Length + 1;
            }
            Console.Write(option + " ");
        }
        Console.WriteLine();
        for (int i = 0; i < tab; i++)
        {
            Console.Write(" ");
        }
        Console.Write("^");
    }
class Option
    {
        public string Name { get; }
        public Action Selected { get; }

        public Option(string name, Action selected)
        {
            Name = name;
            Selected = selected;
        }
    }
Editing is the main code that run the menu and handle the user input
Was this page helpful?