如何在C#Windows窗体中创建重置按钮?

如何在C#Windows窗体中创建重置按钮?,c#,winforms,reset,C#,Winforms,Reset,我正试图在我的C#Windows窗体上设置一个重置按钮。但是,当我用如下代码清除文本框时: textBox1.Clear(); textBox2.Clear(); textBox3.Clear(); textBox4.Clear(); textBox5.Clear(); textBox6.Clear(); 然后,它给出了以下错误:“输入字符串的格式不正确” 这是我的密码: using System;

我正试图在我的C#Windows窗体上设置一个重置按钮。但是,当我用如下代码清除文本框时:

        textBox1.Clear();
        textBox2.Clear();
        textBox3.Clear();
        textBox4.Clear();
        textBox5.Clear();
        textBox6.Clear();
然后,它给出了以下错误:“输入字符串的格式不正确”

这是我的密码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text; 
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Collections;

namespace MidTermPizzas
{
    public partial class Form1 : Form
    {
        pizzaOrder aOrder = new pizzaOrder();

        public Form1()
        {
            InitializeComponent();
        }

        private void formTitle_Click(object sender, EventArgs e)
        {

        }

        //click File, Exit
        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Enjoy your pizza!");
            this.Close();
        }

        //click View, Summary of Orders Placed
        private void summaryOfOrdersToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SummaryOfOrdersPlaced myForm = new SummaryOfOrdersPlaced();
            myForm.Show();
        }


        //amount of Pizzas label
        private void label1_Click(object sender, EventArgs e)
        {

        }

        //form load
        private void Form1_Load(object sender, EventArgs e)
        {

        }

        //sales tax label
        private void label3_Click(object sender, EventArgs e)
        {

        }

        //text in box to the right of "Amount of Pizzas"
        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            aOrder.numberOfPizzas = int.Parse(textBox1.Text);
        }

        //text in box to the right of "Amount of Cokes"
        private void textBox2_TextChanged(object sender, EventArgs e)
        {
            aOrder.numberOfCokes = int.Parse(textBox2.Text);
        }

        //text in box to the right of "Sales Tax"
        private void textBox4_TextChanged(object sender, EventArgs e)
        {

        }

        //File Tool Strip Menu
        private void fileToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        //amount of cokes label
        private void label2_Click(object sender, EventArgs e)
        {

        }

        //reset button
        private void button1_Click_1(object sender, EventArgs e)
        {
            //textBox1.Clear();
            //textBox2.Clear();
            //textBox3.Clear();
            //textBox4.Clear();
            //textBox5.Clear();
            //textBox6.Clear();
         }

        //text in box to the right of "Amount Due"
        private void textBox3_TextChanged_1(object sender, EventArgs e)
        {

        }

        //text in box to the right of "Amount Paid"
        private void textBox5_TextChanged(object sender, EventArgs e)
        {
            aOrder.getAmountPaid = double.Parse(textBox5.Text);
        }

        //click Calculate Change Due button
        private void calculateChangeDue_Click(object sender, EventArgs e)
        {
            textBox6.Text = Convert.ToString(aOrder.GetChangeDue());
        }

        //text in box to right of Change Due
        private void textBox6_TextChanged(object sender, EventArgs e)
        {

        }

        //amount due label
        private void label4_Click(object sender, EventArgs e)
        {

        }

        //amount paid label
        private void label5_Click(object sender, EventArgs e)
        {

        }

        //change due label
        private void label6_Click(object sender, EventArgs e)
        {

        }

        //click Calculate Amount Due
        private void calculateAmountDue_Click(object sender, EventArgs e)
        {
            textBox3.Text = Convert.ToString(aOrder.GetAmountDue());
        }

        //click Calculate Sales Tax
        private void button2_Click(object sender, EventArgs e)
        {
            textBox4.Text = Convert.ToString(aOrder.TaxDue());
        }


    }
}

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

namespace MidTermPizzas
{
    class pizzaOrder
    {
        public int numberOfCokes
        {
            get;

            set;
        }

        public int numberOfPizzas
        {
            get;

            set;
        }

        public double InputOrder()
        {
            const double COKE_PRICE = 1.49;
            const double PIZZA_PRICE = 7.99;
            double inputOrder = (numberOfCokes * COKE_PRICE) + (numberOfPizzas * PIZZA_PRICE);
            return inputOrder;
        }

        public double TaxDue()
        {
            const double TAX = .073;
            double taxDue = (this.InputOrder() * TAX);
            return taxDue;
        }

        public double GetAmountDue()
        {
            double getAmountDue = this.InputOrder() + this.TaxDue();
            return getAmountDue;
        }

        public double getAmountPaid
        { get; set; }

        public double GetChangeDue()
        {
            double getChangeDue = this.getAmountPaid - this.GetAmountDue();
            return getChangeDue;
        }
    }
}
对其他文本框执行相同的操作,您应该对所有文本框使用
TryParse
,并使用1
TextChanged
事件处理程序

以下代码使用
TryParse
,并假设如果解析失败,默认值将为
0

private void textBox1_TextChanged(object sender, EventArgs e){
  int v;
  if(int.TryParse(textBox1.Text, out v)){
    aOrder.numberOfPizzas = v;
  } else aOrder.numberOfPizzas = 0;
}

按照其他答案的建议使用TryParse,或者在clear方法中为用于数字输入的文本框放置0。另一种方法是用于数字输入的文本框。

这里您也可以使用手动方法。在按钮单击函数中编写以下代码

if (textBox1.Text != string.Empty || textBox2.Text != string.Empty || textBox3.Text != string.Empty || textBox4.Text != string.Empty || textBox5.Text != string.Empty || textBox6.Text != string.Empty)
            {
                textBox1.Text = "";
                textBox2.Text = "";
                textBox3.Text = "";
                textBox4.Text = "";
                textBox5.Text = "";
                textBox6.Text = "";
            }

不要在TextChanged事件处理程序中调用double.Parse()。使用TryParse()并检查是否有空字符串。与您的问题无关,但您应该清除所有未实现的单击方法。双击设计器中的项目时,会添加这些选项。
if (textBox1.Text != string.Empty || textBox2.Text != string.Empty || textBox3.Text != string.Empty || textBox4.Text != string.Empty || textBox5.Text != string.Empty || textBox6.Text != string.Empty)
            {
                textBox1.Text = "";
                textBox2.Text = "";
                textBox3.Text = "";
                textBox4.Text = "";
                textBox5.Text = "";
                textBox6.Text = "";
            }