【C#控制台】实现光标选择选项

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace demo
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("\n\n\n");
            Console.WriteLine("\t1.菜单1");
            Console.WriteLine("\t1.菜单2");
            Console.WriteLine("\t1.菜单3");


            int o=CP.CPwrite(4,0,3);

            Console.WriteLine(o);

            Console.SetCursorPosition(0,7);  //设置光标位置  列,行

            Console.ReadLine();
        }
    }
    class CP
    {
        public static int CPwrite(int top,int left,int n)
        {
            Console.SetCursorPosition(left,top);        //设置光标位置
            int oldtop=top;
            bool tag = true;
            do
            {
                ConsoleKeyInfo info = Console.ReadKey();
                switch (info.Key)
                {
                    case ConsoleKey.Enter:
                        tag = false;
                        break;
                    case ConsoleKey.UpArrow:
                        if (top > oldtop && top <= oldtop+n - 1)
                        {
                            top -= 1;
                            Console.SetCursorPosition(left, top);
                            //Console.Write(top);
                        }
                        else
                        {
                            Console.SetCursorPosition(left, top);
                        }
                        break;
                    case ConsoleKey.DownArrow:
                        if (top >= oldtop && top < oldtop+n - 1)
                        {
                            top += 1;
                            Console.SetCursorPosition(left, top);
                            //Console.Write(top);
                        }
                        else
                        {
                            Console.SetCursorPosition(left, top);
                        }
                        break;
                    default:
                        Console.SetCursorPosition(left, top);
                        break;
                }
            } while (tag);
            return top+1-oldtop;
        }
    }
}

猜你喜欢

转载自blog.csdn.net/qq_41138935/article/details/80831474