我正在尝试将重置按钮设为我的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;
}
}
}
答案 0 :(得分:1)
private void textBox1_TextChanged(object sender, EventArgs e){
aOrder.numberOfPizzas = textBox1.Text == "" ? 0 : int.Parse(textBox1.Text);
}
对其他textBox执行相同的操作,您应该使用TryParse
并对所有textBox使用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;
}
答案 1 :(得分:0)
按照其他答案的建议使用TryParse,或者在clear方法中将0用于数字输入的文本框。另一种方法是将MaskedTextBox用于用于数字输入的文本框。
答案 2 :(得分: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 = "";
}