简体   繁体   中英

how to create a zero button in calculator in windows form calculator

I am a student. I have a trouble in creating my Zero Button. The scenario is this, we should create a zero button and when you run and click it, it should not show excess Zeros just like this ... 000000000 ... up to how may times you click the zero button. I want to know how to create a Zero button and when you run and click it, it shows only one Zero (0). Our teacher said that there is no number like this ... 00001.111 or 000.15 or 0005 or anything that starts with a lot of Zero. But I still want my Zero button to function like this ... 100 or 5000 or 0.0006 and all kinds of multiple Zero in a number. Thanks a lot!

Thanks everyone. This is my wrong solution.

private void Form1_Load(object sender, EventArgs e)
    {
    //    oneBtn.Click += new EventHandler(btn_Click);
    //    twoBtn.Click += new EventHandler(btn_Click);
    //    threeBtn.Click += new EventHandler(btn_Click);
    //    fourBtn.Click += new EventHandler(btn_Click);
    //    fiveBtn.Click += new EventHandler(btn_Click);
    //    sixBtn.Click += new EventHandler(btn_Click);
    //    sevenBtn.Click += new EventHandler(btn_Click);
    //    eightBtn.Click += new EventHandler(btn_Click);
    //    nineBtn.Click += new EventHandler(btn_Click);
        zeroBtn.Click += new EventHandler(btn_Click);
    //    pointBtn.Click += new EventHandler(btn_Click);
    }

and

 void btn_Click(object sender, EventArgs e)
    {
        try
        {
            Button btn = sender as Button;

            switch (btn.Name)
            {
    //            case "oneBtn":
    //                textBox_output.Text += "1";
    //                break;
    //            case "twoBtn":
    //                textBox_output.Text += "2";
    //                break;
    //            case "threeBtn":
    //                textBox_output.Text += "3";
    //                break;
    //            case "fourBtn":
    //                textBox_output.Text += "4";
    //                break;
    //            case "fiveBtn":
    //                textBox_output.Text += "5";
    //                break;
    //            case "sixBtn":
    //                textBox_output.Text += "6";
    //                break;
    //            case "sevenBtn":
    //                textBox_output.Text += "7";
    //                break;
    //            case "eightBtn":
    //                textBox_output.Text += "8";
    //                break;
    //            case "nineBtn":
    //                textBox_output.Text += "9";
    //                break;
                case "zeroBtn":
                    textBox_output.Text += "0";
                    break;
    //            case "pointBtn":
    //                if (!textBox_output.Text.Contains("."))
    //                    textBox_output.Text += ".";
    //                break;
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show("Sorry for the inconvenience, Unexpected error occured. Details:" +
                ex.Message);
        }
    }

and this is the keypress

private void textBox_output_KeyPress(object sender, KeyPressEventArgs e)
    {
        int keycode;
        keycode = e.KeyChar;
        if (keycode >= 48 && keycode <= 57 || keycode == 8 || keycode == 32 || keycode == 46)
        {
            if (keycode == 46)
                ++trackkeypoint;
            if (trackkeypoint > 1)
            {
                if (e.KeyChar == '.' && (sender as TextBox).Text.IndexOf('.') > -1)
                {
                    e.Handled = true;
                    --trackkeypoint;
                }
            }
        }
        else e.Handled = true;
    }

In the button click(zero button) you should check two things. First if you have separator '.' which you do with IndexOf method of string. If this return > 0 you have "." and should add 0. After that if you don't have "." in the else method check if the string doesn't have value and add zero if it is true. If the string is not empty and doesn't start with 0 again add zero.

case "zeroBtn":
if(textBox_output.Text.IndexOf(".") > 0)
{
    textBox_output.Text+="0";
}
else
{
    //edit: thanks for the comment
    if(textBox_output.Text.ToString() == "")
    {
         textBox_output.Text+="0";
    }
    else if(!textBox_output.Text.StartWith("0"))
    {
        textBox_output.Text+="0";
    }
    else
    {
        //do nothing
    }
}

Hope this helps.

About your edit when you are using the key: You should make the same checks when the keycode == 48 ( I think this was the code for 0).

I presume you are using winforms but you need to add extra handling for your zero button, something like the following

//If textbox starts doesn't start with 0 or Textbox
//contains a decimal point then it is ok to add a zero
if(!textBox_output.Text.StartsWith("0") || textBox_output.Text.Contains("."))
    textBox_output.Text += "0";

You could also add extra error handling for when the textbox is empty to allow for a zero to be added, as well as other handling on the other numbers to replace an existing zero with the number

if(textBox_output.Text.Length <= 1)
    textBox_output.Text = "1";
else
    textBox_output.Text += "1";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM