本文介绍了文本框出错 - 输入字符串格式不正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
美好的一天,
我已经在64位操作系统上开发和应用,WIN 8,它运行完美但在我安装32位的应用程序后,WIN 7机器我遇到错误老实说,我觉得很奇怪。我在Visiual工作室选择了Release和86x。您的紧急回复将不胜感激
我的代码
Good Day,
I have developed and application on a 64 bit OS, WIN 8, it is running perfect but after I installing the app on 32 bit, WIN 7 machine I experience an error which I honestly find it weird. I have selected "Release" and "86x" on my Visiual studio. Your urgent response will be appreciated
my Code
namespace AfricanFoodsPOS
{
public partial class Payment : Form
{
private BindingList<tblproduct> products = new BindingList<tblproduct>();
AFStagingEntities afNewObject = new AFStagingEntities();
public delegate void PaymentMadeEvent(object sender, PaymentMadeEventArgs e);
//public decimal myValue = 0;
public event PaymentMadeEvent PaymentMade;
public decimal myPay;
public decimal MyPay
{
get { return myPay; }
set {
myPay = value;
txtYouArePaying.Text = String.Format("{0:c}", myPay);
}
}
public decimal paymentAmount;
public decimal PaymentAmount
{
get { return paymentAmount; }
set
{
paymentAmount = value;
txtAmountToPay.Text = String.Format("{0:c}", paymentAmount);
}
}
public Payment()
{
InitializeComponent();
}
//was private
public void PaymentHasBeenMade(object sender, EventArgs e)
{
decimal total = 0;
//robust
try
{
decimal YouArePaying = 0;
decimal AmountToPay = 0;
YouArePaying = decimal.Parse(txtYouArePaying.Text.TrimStart('R'));
AmountToPay = decimal.Parse(txtAmountToPay.Text.TrimStart('R'));
total = YouArePaying - AmountToPay;
if (total >= 0)
{
MessageBox.Show("Give change: R " + total);
PaymentMade(this, new PaymentMadeEventArgs() { PaymentSuccess = true });
}
else
{
total = AmountToPay - YouArePaying;
txtAmountToPay.Text = total.ToString();
MessageBox.Show("Customer is: R " + total + " short.");
PaymentMade(this, new PaymentMadeEventArgs() { PaymentSuccess = false });
}
}
catch (Exception exx)
{
MessageBox.Show("An error has occured, please enter a valid amount, " + exx.Message);
}
finally
{
//txtYouArePaying.Dispose();
}
}
private void Payment_Load(object sender, EventArgs e)
{
}
}
public class PaymentMadeEventArgs : EventArgs
{
private bool paymentSuccess;
public bool PaymentSuccess
{
get { return paymentSuccess; }
set { paymentSuccess = value; }
}
}
}
推荐答案
string alphanum = "123.abc";
string justnum = "123.456";
double result; //Don't bother setting the out parameter. It will always change in the tryparse
if(double.TryParse(alphanum,out result){
Assert.Fail("The parse should return false);
}
Assert.AreEqual(0.0,result); //whatever the 'result' was set to before, it is now zero
if(double.TryParse(justnum ,out result){
Assert.AreEqual(123.456,result);
}
使用它你可以在使用它之前检查你的数据。所以如果tryparse失败,告诉用户它的格式是错误的
我希望有帮助^ _ ^
Andy
Using this you can check your data before using it. So if the tryparse fails, tell the user that the format it wrong
I hope that helps ^_^
Andy
这篇关于文本框出错 - 输入字符串格式不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!