【问题标题】:How can I make a reset button in my C# Windows Form?如何在我的 C# Windows 窗体中设置重置按钮?
【发布时间】:2013-11-13 07:25:50
【问题描述】:

我正在尝试在我的 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;
        }
    }
}

【问题讨论】:

  • 不要在 TextChanged 事件处理程序中调用 double.Parse()。使用 TryParse() 并检查是否有空字符串。
  • 与您的问题无关,但您应该摆脱所有未实现的 _Click 方法。当您在设计器中双击一个项目时,这些会被添加。

标签: c# winforms reset


【解决方案1】:
private void textBox1_TextChanged(object sender, EventArgs e){
  aOrder.numberOfPizzas = textBox1.Text == "" ? 0 : int.Parse(textBox1.Text);
}

对其他文本框做同样的事情,你应该使用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;
}

【讨论】:

    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

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

      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 = "";
                  }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-07-20
        • 2012-01-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-03-12
        • 2010-11-11
        相关资源
        最近更新 更多