更新了C#多个SetCursorPosition,但第一个被跳过了

时间:2019-09-14 10:14:36

标签: c#

因此,昨天我对在c# multiple SetCursorPosition but first one gets skipped处找到的多个文本条目多次调用SetCursorPosition提出了相同的问题。
我发现用来调用包含设置游标的方法的SwitchMenu()方法是使第一个SetCursorPosition()被跳过的罪魁祸首。如果有人想进行测试并找出问题所在,我在下面提供了代码。首次调用方法时的预期结果是:
(标题)
名:(此处为光标)
姓氏:
地址:
电话:
电子邮件:

PS切换菜单中唯一可用的选项是1(正在讨论的那个)并退出。
非常感谢您的帮助。

 class Program
 {
    private Bank bank = new Bank();


    static void Main(string[] args)
    {
        new Program().SwitchMenu();

    }

    public void SwitchMenu()
    {
        char numberEntered;

        //call a different function depending on which number the user chose
        // '}' is a dummy value
        while ((numberEntered = EnteredChar()) != '}')
        {
            switch (numberEntered)
            {
                case '1': bank.createAccount(); break;
                case '2': bank.searchAccount(); break;
                case '3': bank.deposit(); break;
                case '4': bank.withdraw(); break;
                case '5': bank.accountStatement(); break;
                case '6': bank.deleteAccount(); break;
                case '7': Environment.Exit(0); break;

            }
        }
        //Console.SetCursorPosition(0, Console.CursorTop-2);
        //Console.Write(new string(' ', Console.WindowWidth));


    }

    public char EnteredChar()
    {
        Console.Clear();
        String heading1 = "Welcome to the Simple Banking Application";

        //Write all needed text to console
        Console.WriteLine(String.Format("{0," + ((Console.WindowWidth / 2) + heading1.Length / 2) + "}", heading1));
        Console.WriteLine("\n\t\t1. Create a new account\n\t\t2. Search for an account\n\t\t3. Deposit\n\t\t4. Withdraw\n\t\t5. Account Statement\n\t\t6. Delete Account\n\t\t7. Exit");
        Console.Write("\n\t\t   Enter your choice(1 - 7):");
        char charEntered = Convert.ToChar(Console.Read());
        return charEntered;
    }
}

class Bank
{
    private int accountNumber = 100000;
    public void createAccount()
    {
        Console.Clear();
        String heading1 = "CREATE A NEW ACCOUNT";
        String heading2 = "ENTER THE DETAILS";
        String firstNameLbl = "First Name:";
        String lastNameLbl = "Last Name:";
        String addressLbl = "Address:";
        String phoneLbl = "Phone Number:";
        String emailLbl = "Email Address:";

        Console.WriteLine(String.Format("{0," + ((Console.WindowWidth / 2) + heading1.Length / 2) + "}", heading1));
        Console.WriteLine(String.Format("{0," + ((Console.WindowWidth / 2) + heading2.Length / 2) + "}", heading2));

        Console.Write("\n" + String.Format("{0," + ((Console.WindowWidth / 4) + firstNameLbl.Length - 3) + "}", firstNameLbl));
        Console.Write("\n" + String.Format("{0," + ((Console.WindowWidth / 4) + lastNameLbl.Length - 3) + "}", lastNameLbl));
        Console.Write("\n" + String.Format("{0," + ((Console.WindowWidth / 4) + addressLbl.Length - 3) + "}", addressLbl));
        Console.Write("\n" + String.Format("{0," + ((Console.WindowWidth / 4) + phoneLbl.Length - 3) + "}", phoneLbl));
        Console.Write("\n" + String.Format("{0," + ((Console.WindowWidth / 4) + emailLbl.Length - 3) + "}", emailLbl));

        Console.SetCursorPosition((Console.WindowWidth / 4) + firstNameLbl.Length - 3, 3);
        String firstName = Console.ReadLine();

        Console.SetCursorPosition((Console.WindowWidth / 4) + lastNameLbl.Length - 3, 4);
        String lastName = Console.ReadLine();

        Console.SetCursorPosition((Console.WindowWidth / 4) + addressLbl.Length - 3, 5);
        String address = Console.ReadLine();

        Console.SetCursorPosition((Console.WindowWidth / 4) + phoneLbl.Length - 3, 6);
        int phoneNumber = Convert.ToInt32(Console.ReadLine());

        Console.SetCursorPosition((Console.WindowWidth / 4) + emailLbl.Length - 3, 7);
        String email = Console.ReadLine();

        Console.Write("Is the information correct? (y/n)");
        char confirmChar = Convert.ToChar(Console.Read());
        if (confirmChar == 'y')
        {
            incrementAccountNumber();
            //accounts.Add(new Account(firstName, lastName, address, phoneNumber, email, accountNumber));

            Console.WriteLine("Account Created! Details will be provided via email.");

            Console.WriteLine("Account number is: {0}", accountNumber);
            Console.Write("Press Enter to return to menu.");
            ConsoleKeyInfo enterKey = Console.ReadKey(true);
            if (enterKey.Key == ConsoleKey.Enter)
            {
                Console.Clear();
            }

        }
        else if (confirmChar == 'n')
        {
            Console.Clear();
            createAccount();
        }


    }

    public void searchAccount()
    {

    }
    public void deposit()
    {

    }
    public void withdraw()
    {

    }
    public void accountStatement()
    {

    }
    public void deleteAccount()
    {

    }

    public void incrementAccountNumber()
    {
        ++accountNumber;
    }

}

0 个答案:

没有答案