C# 错误系统格式异常:"输入字符串格式不正确。



我有一个简单的表单,它有几个字段,它们都是相互计算的,并且是需要数字输入的文本框。我遇到了一个问题,当我提交表单时,即使文本框只包含数字,我也会收到以下错误。

错误:

System.FormatException:"输入字符串的格式不正确。">

This exception was originally thrown at this call stack:
System.Number.StringToNumber(string, System.Globalization.NumberStyles, ref System.Number.NumberBuffer, System.Globalization.NumberFormatInfo, bool)
System.Number.ParseDecimal(string, System.Globalization.NumberStyles, System.Globalization.NumberFormatInfo)
System.Convert.ToDecimal(string)
CTMS.Fuel.fuelFromTextBox_TextChanged(object, System.EventArgs) in Fuel.cs
System.Windows.Forms.Control.OnTextChanged(System.EventArgs)
System.Windows.Forms.TextBoxBase.OnTextChanged(System.EventArgs)
System.Windows.Forms.Control.Text.set(string)
System.Windows.Forms.TextBoxBase.Text.set(string)
System.Windows.Forms.TextBox.Text.set(string)
CTMS.Fuel.AddFuelButton_Click(object, System.EventArgs) in Fuel.cs
...
[Call Stack Truncated]

从这里:

private void fuelFromTextBox_TextChanged(object sender, EventArgs e)
{
decimal parsedValue;
if (!decimal.TryParse(fuelToTextBox.Text, out parsedValue))
{
}
else
{
mileageMinusTextBox.Text = (Convert.ToDecimal(fuelToTextBox.Text) - Convert.ToDecimal(fuelFromTextBox.Text)).ToString();
}
decimal parsedValue2;
if (!decimal.TryParse(fuelLitersTextBox.Text, out parsedValue2))
{
}
else
{
fuelGallonsTextBox.Text = (Convert.ToDecimal(fuelLitersTextBox.Text) / Convert.ToDecimal(convTextBox.Text)).ToString();
}
Decimal parsedValue3;
if (!Decimal.TryParse(fuelGallonsTextBox.Text, out parsedValue3))
{
}
else
{
fuelMPGTextBox.Text = Math.Round((Convert.ToDecimal(mileageMinusTextBox.Text) / Convert.ToDecimal(fuelGallonsTextBox.Text)), 3).ToString();
}
fuelFromTextBox.Refresh();
fuelToTextBox.Refresh();
fuelLitersTextBox.Refresh();
fuelGallonsTextBox.Refresh();
fuelMPGTextBox.Refresh();
mileageMinusTextBox.Refresh();
}

完整代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace CTMS
{
public partial class Fuel : Form
{
public Fuel()
{
InitializeComponent();
}

private void Fuel_Load(object sender, EventArgs e)
{
this.fuelTableAdapter.Fill(this.cTDBDataSet.fuel);
SqlConnection con = new SqlConnection(Properties.Settings.Default.CTDBCS);
convTextBox.Text = "4.546";
con.Open();
if (con.State == ConnectionState.Open)
{
connectedPic.Visible = true;
disconnectedPic.Visible = false;
panel1.BackColor = Color.LightGreen;
}
else
{
connectedPic.Visible = false;
disconnectedPic.Visible = true;
panel1.BackColor = Color.OrangeRed;
}
string Sql = "SELECT DISTINCT reg FROM vehicles";
SqlCommand cmd = new SqlCommand(Sql, con);
SqlDataReader DR = cmd.ExecuteReader();
while (DR.Read())
{
fuelReg.Items.Add(DR[0]);
fuelReg.SelectedIndex = -1;
fuelRegView.Items.Add(DR[0]);
fuelRegView.SelectedIndex = -1;
}
con.Close();
mileageMinusTextBox.Text = "0";
}

private void AddFuelButton_Click(object sender, EventArgs e)
{
fuelDateTimeCombined.Text = fuelDatePicker.Text + ' ' + fuelTimePicker.Text;
int currmile; 
int prevmile;
Int32.TryParse(fuelToTextBox.Text, out currmile);
Int32.TryParse(fuelFromTextBox.Text, out prevmile);
if (string.IsNullOrWhiteSpace(fuelReg.Text) || string.IsNullOrWhiteSpace(fuelFromTextBox.Text) || string.IsNullOrWhiteSpace(fuelLitersTextBox.Text) ||
currmile < prevmile)

{
MessageBox.Show("Values Missing or Mileage less than previous", "", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else
{
SqlConnection con = new SqlConnection(Properties.Settings.Default.CTDBCS);
con.Open();
string Sql = "INSERT INTO fuel (fuelreg, fuelprevmile, fuelcurrentmile, fuellitres, fuelmpg, fueldatetime)"
+ "VALUES("
+ "'" + fuelReg.Text + "', "
+ "'" + fuelFromTextBox.Text + "', "
+ "'" + fuelToTextBox.Text + "', "
+ "'" + fuelLitersTextBox.Text + "', "
+ "'" + fuelMPGTextBox.Text + "', "
+ "'" + fuelDateTimeCombined.Text + "'"
+ "); ";
SqlCommand cmd = new SqlCommand(Sql, con);
SqlDataReader DR = cmd.ExecuteReader();
con.Close();
fuelReg.Text = null;
fuelFromTextBox.Text = null;
fuelToTextBox.Text = null;
fuelLitersTextBox.Text = null;
fuelMPGTextBox.Text = null;
fuelDatePicker.Text = null;
fuelTimePicker.Text = null;
fuelDateTimeCombined.Text = null;
this.fuelTableAdapter.Fill(this.cTDBDataSet.fuel);
}
}
private void FuelDateNowButton_Click(object sender, EventArgs e)
{
fuelDatePicker.Text = DateTime.Now.ToString();
fuelTimePicker.Text = DateTime.Now.ToString();
}
private void FuelPrevMileage_Click(object sender, EventArgs e)
{
}
private void FuelReg_SelectedIndexChanged(object sender, EventArgs e)
{
fuelFromTextBox.ReadOnly = false;
fuelToTextBox.ReadOnly = false;
fuelLitersTextBox.ReadOnly = false;
}
private void FuelBindingNavigatorSaveItem_Click(object sender, EventArgs e)
{
this.Validate();
this.fuelBindingSource.EndEdit();
this.tableAdapterManager.UpdateAll(this.cTDBDataSet);
}
private void mileageMinusTextBox_TextChanged(object sender, EventArgs e)
{
decimal parsedValue2;
if (!decimal.TryParse(fuelLitersTextBox.Text, out parsedValue2))
{
}
else
{
fuelGallonsTextBox.Text = (Convert.ToDecimal(fuelLitersTextBox.Text) / Convert.ToDecimal(convTextBox.Text)).ToString();
}
fuelFromTextBox.Refresh();
fuelToTextBox.Refresh();
fuelLitersTextBox.Refresh();
fuelGallonsTextBox.Refresh();
fuelMPGTextBox.Refresh();
mileageMinusTextBox.Refresh();
}
private void FuelLitersTextBox_TextChanged(object sender, EventArgs e)
{
decimal parsedValue;
if (!decimal.TryParse(fuelLitersTextBox.Text, out parsedValue))
{
}
else
{
fuelGallonsTextBox.Text = (Convert.ToDecimal(fuelLitersTextBox.Text) / Convert.ToDecimal(convTextBox.Text)).ToString();
}
fuelFromTextBox.Refresh();
fuelToTextBox.Refresh();
fuelLitersTextBox.Refresh();
fuelGallonsTextBox.Refresh();
fuelMPGTextBox.Refresh();
mileageMinusTextBox.Refresh();
}

private void fuelToTextBox_TextChanged(object sender, EventArgs e)
{
int parsedValue;
if (!int.TryParse(fuelToTextBox.Text, out parsedValue))
{
}
else
{
mileageMinusTextBox.Text = (Convert.ToDecimal(fuelToTextBox.Text) - Convert.ToDecimal(fuelFromTextBox.Text)).ToString();
}
decimal parsedValue2;
if (!decimal.TryParse(fuelLitersTextBox.Text, out parsedValue2))
{
}
else
{
fuelGallonsTextBox.Text = (Convert.ToDecimal(fuelLitersTextBox.Text) / Convert.ToDecimal(convTextBox.Text)).ToString();
}
Decimal parsedValue3;
if (!Decimal.TryParse(fuelGallonsTextBox.Text, out parsedValue3))
{
}
else
{
fuelMPGTextBox.Text = Math.Round((Convert.ToDecimal(mileageMinusTextBox.Text) / Convert.ToDecimal(fuelGallonsTextBox.Text)), 3).ToString();
}
fuelFromTextBox.Refresh();
fuelToTextBox.Refresh();
fuelLitersTextBox.Refresh();
fuelGallonsTextBox.Refresh();
fuelMPGTextBox.Refresh();
mileageMinusTextBox.Refresh();
}
private void fuelGallonsTextBox_TextChanged(object sender, EventArgs e)
{
Decimal parsedValue;
if (!Decimal.TryParse(fuelGallonsTextBox.Text, out parsedValue))
{
}
else
{
fuelMPGTextBox.Text = Math.Round((Convert.ToDecimal(mileageMinusTextBox.Text) / Convert.ToDecimal(fuelGallonsTextBox.Text)),3).ToString();
}
fuelFromTextBox.Refresh();
fuelToTextBox.Refresh();
fuelLitersTextBox.Refresh();
fuelGallonsTextBox.Refresh();
fuelMPGTextBox.Refresh();
mileageMinusTextBox.Refresh();
}
private void fuelRegView_SelectedIndexChanged(object sender, EventArgs e)
{
BindingSource bs = new BindingSource();
bs.DataSource = fuelDataGridView.DataSource;
bs.Filter = "fuelreg like '%" + fuelRegView.Text + "%'";
fuelDataGridView.DataSource = bs;

}
private void tabControl1_Click(object sender, EventArgs e)
{
BindingSource bs = new BindingSource();
bs.DataSource = fuelDataGridView.DataSource;
bs.Filter = "fuelreg like '%'";
fuelDataGridView.DataSource = bs;
fuelRegView.SelectedIndex = -1;
}
private void fuelFromTextBox_TextChanged(object sender, EventArgs e)
{
decimal parsedValue;
if (!decimal.TryParse(fuelToTextBox.Text, out parsedValue))
{
}
else
{
mileageMinusTextBox.Text = (Convert.ToDecimal(fuelToTextBox.Text) - Convert.ToDecimal(fuelFromTextBox.Text)).ToString();
}
decimal parsedValue2;
if (!decimal.TryParse(fuelLitersTextBox.Text, out parsedValue2))
{
}
else
{
fuelGallonsTextBox.Text = (Convert.ToDecimal(fuelLitersTextBox.Text) / Convert.ToDecimal(convTextBox.Text)).ToString();
}
Decimal parsedValue3;
if (!Decimal.TryParse(fuelGallonsTextBox.Text, out parsedValue3))
{
}
else
{
fuelMPGTextBox.Text = Math.Round((Convert.ToDecimal(mileageMinusTextBox.Text) / Convert.ToDecimal(fuelGallonsTextBox.Text)), 3).ToString();
}
fuelFromTextBox.Refresh();
fuelToTextBox.Refresh();
fuelLitersTextBox.Refresh();
fuelGallonsTextBox.Refresh();
fuelMPGTextBox.Refresh();
mileageMinusTextBox.Refresh();
}
}
}

重写所有Convert以使用TryParse

private void fuelFromTextBox_TextChanged(object sender, EventArgs e)
{
double from, to;
if (
!double.TryParse(fuelFromTextBox.Text, out from) ||
!double.TryParse(fuelToTextBox.Text, out to)
)
{
return;
}
mileageMinusTextBox.Text = (to - from).ToString();
decimal mile, gall;
if (
!decimal.TryParse(mileageMinusTextBox.Text, out mile) ||
!decimal.TryParse(fuelGallonsTextBox.Text, out gall)
)
{
return;
}
fuelMPGTextBox.Text = Math.Round((mile / gall), 3).ToString();
decimal litres, conv;
if (
!decimal.TryParse(fuelLitersTextBox.Text, out litres) ||
!decimal.TryParse(convTextBox.Text, out conv)
)
{
return;
}
fuelGallonsTextBox.Text = (litres / conv).ToString();
fuelFromTextBox.Refresh();
fuelToTextBox.Refresh();
fuelLitersTextBox.Refresh();
fuelGallonsTextBox.Refresh();
fuelMPGTextBox.Refresh();
mileageMinusTextBox.Refresh();
fuelFromTextBox.Update();
fuelToTextBox.Update();
fuelLitersTextBox.Update();
fuelGallonsTextBox.Update();
fuelMPGTextBox.Update();
mileageMinusTextBox.Update();
}

最新更新