Нужна помощь, чтобы избавиться от ведущих нулей и лучше использовать кнопку фокусировки в c# Visual Studio - PullRequest
0 голосов
/ 11 марта 2020

Я создал большую часть кода для простого калькулятора с GUI, равным C#. У меня сейчас проблемы с очисткой кода. Я хочу

  1. Поместить одиночный ноль в текстовое поле ответа при запуске приложения и при нажатии кнопки очистки.

  2. Сфокусируйте go на кнопке очистки при запуске приложения и при нажатии кнопки «=» на равных.

  3. не разрешать начальные нули. Пример: 0345.

Это мой код:

namespace calculator {
    public partial class Form1: Form 
    {
        double FirstNunber;
        public Form1() 
        {
            InitializeComponent();
        }
        private string sOPerator;
        private bool bOperatorClicked;
        private double FirstNum = 0;
        private void panel1_Paint(object sender, PaintEventArgs e) {}
        private void button2_Click(object sender, EventArgs e) 
        {
            if (bOperatorClicked == true)
                txtAnswer.Text = ".";
            else
                txtAnswer.Text += ".";
            bOperatorClicked = false;
        }
        private void button6_Click(object sender, EventArgs e) 
        {
            if (bOperatorClicked == true)
                txtAnswer.Text = "5";
            else
                txtAnswer.Text += "5";
            bOperatorClicked = false;
        }
        private void button10_Click(object sender, EventArgs e) 
        {
            if (bOperatorClicked == true)
                txtAnswer.Text = "3";
            else
                txtAnswer.Text += "3";
            bOperatorClicked = false;
        }
        private void panel2_Paint(object sender, PaintEventArgs e) {}
        private void btn1_Click(object sender, EventArgs e) 
        {
            if (bOperatorClicked == true)
                txtAnswer.Text = "1";
            else
                txtAnswer.Text += "1";
            bOperatorClicked = false;
        }
        private void btn7_Click(object sender, EventArgs e) 
        {
            if (bOperatorClicked == true)
                txtAnswer.Text = "7";
            else
                txtAnswer.Text += "7";
            bOperatorClicked = false;
        }
        private void btn0_Click(object sender, EventArgs e) 
        {
            if (bOperatorClicked == true)
                txtAnswer.Text = "0";
            else
                txtAnswer.Text += "0";
            bOperatorClicked = false;
        }
        private void btn2_Click(object sender, EventArgs e) 
        {
            if (bOperatorClicked == true)
                txtAnswer.Text = "2";
            else
                txtAnswer.Text += "2";
            bOperatorClicked = false;
        }
        private void btn6_Click(object sender, EventArgs e) 
        {
            if (bOperatorClicked == true)
                txtAnswer.Text = "6";
            else
                txtAnswer.Text += "6";
            bOperatorClicked = false;
        }
        private void btn4_Click(object sender, EventArgs e) 
        {
            if (bOperatorClicked == true)
                txtAnswer.Text = "4";
            else
                txtAnswer.Text += "4";
            bOperatorClicked = false;
        }
        private void btn8_Click(object sender, EventArgs e) 
        {
            if (bOperatorClicked == true)
                txtAnswer.Text = "8";
            else
                txtAnswer.Text += "8";
            bOperatorClicked = false;
        }
        private void btn9_Click(object sender, EventArgs e) 
        {
            if (bOperatorClicked == true)
                txtAnswer.Text = "9";
            else
                txtAnswer.Text += "9";
            bOperatorClicked = false;
        }
        private void btnClear_Click(object sender, EventArgs e) 
        {
            txtAnswer.Clear();
        }
        private void btnOff_Click(object sender, EventArgs e) 
        {
            Application.Exit();
        }
        private void btnAdd_Click(object sender, EventArgs e) 
        {
            bOperatorClicked = true;
            sOPerator = "+";
            FirstNum = Convert.ToDouble(txtAnswer.Text);
        }
        private void btnMinus_Click(object sender, EventArgs e) 
        {
            bOperatorClicked = true;
            sOPerator = "-";
            FirstNum = Convert.ToDouble(txtAnswer.Text);
        }
        private void btnMultiply_Click(object sender, EventArgs e) 
        {
            bOperatorClicked = true;
            sOPerator = "*";
            FirstNum = Convert.ToDouble(txtAnswer.Text);
        }
        private void btnDivide_Click(object sender, EventArgs e) 
        {
            bOperatorClicked = true;
            sOPerator = "/";
            FirstNum = Convert.ToDouble(txtAnswer.Text);
        }
        private void btnEqual_Click(object sender, EventArgs e) 
        {
            double SecondNumber;
            double Result;
            SecondNumber = Convert.ToDouble(txtAnswer.Text);
            if (sOPerator == "+") 
            {
                Result = (FirstNum + SecondNumber);
                txtAnswer.Text = Convert.ToString(Result);
                FirstNum = Result;
            }
            if (sOPerator == "-")  
            {
                Result = (FirstNum - SecondNumber);
                txtAnswer.Text = Convert.ToString(Result);
                FirstNum = Result;
            }
            if (sOPerator == "*") 
            {
                Result = (FirstNum * SecondNumber);
                txtAnswer.Text = Convert.ToString(Result);
                FirstNum = Result;
            }
            if (sOPerator == "/") 
            {
                if (SecondNumber == 0)  
                {
                    txtAnswer.Text = ("Cannot divide by zero");
                } 
                else 
                {
                    Result = (FirstNum / SecondNumber);
                    txtAnswer.Text = Convert.ToString(Result);
                    FirstNum = Result;
                }
            }
        }
        private void textBox1_TextChanged(object sender, EventArgs e) 
        {
            txtAnswer = txtAnswer;
        }
        private void button5_Click(object sender, EventArgs e) 
        {
            if (!txtAnswer.Text.Contains("."))
                txtAnswer.Text += ".";
        }
    }
}
...