C#.net中的税收计算错误 [英] Errors on Tax Calculation in C# .net
本文介绍了C#.net中的税收计算错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
你好,
Hello,
private void textBox2_TextChanged(object sender, EventArgs e)
{
float t;
float g;
t = float.Parse(textBox3.Text);
g = float.Parse(textBox10.Text);
float j;
j = g * (t / 100) + g;
Convert.ToDecimal(textBox2.Text = j.ToString());
}
这里输入字符串不正确textbox3.text中的格式错误如何解决帮助我......
Here input string is not correct format error in textbox3.text how to resolve Help me......
推荐答案
让您的用户输入正确的值?
或者使用正确的文本框?
您正在处理textBox2事件,但是您没有; t引用textBox2 - 我怀疑你的意思是2而不是3.
但即便如此,这对待用户也是一种糟糕的方式。
在使用值之前,请检查它并向用户报告问题:此时,如果他将字段留空,则程序崩溃。如果他发错并得到一个逗号而不是一个点,它就会崩溃。如果他输入他的名字,它会崩溃。那真的很差 - 它会让你烦恼,不是吗?
试试这个:
Get your user to type correct values?
Or use the right textbox?
You are handling the textBox2 event, but you don;t reference textBox2 at all - I suspect you meant 2 rather than 3.
But even then, that is a poor way to treat users.
Before you use a value, check it, and report problems to your user: at the moment, if he leaves a field blank your program crashes. If he miskeys and gets a comma instead of a dot, it crashes. If he types his name, it crashes. That really is poor - it would annoy the heck out of you, wouldn't it?
Try this:
private void textBox2_TextChanged(object sender, EventArgs e)
{
float t;
float g;
if (!float.TryParse(textBox3.Text, out t))
{
MessageBox.Show("Please type a number in textbox 3!");
return;
}
if (!float.TryParse(textBox10.Text, out g))
{
MessageBox.Show("Please type a number in textbox 10!");
return;
}
float j = g * (t / 100) + g;
string result = j.ToString();
if (textBox2.Text != result)
{
textBox2.Text = result;
}
}
注意底部的位:如果你每次只设置TextBox2.Text的值,它会立即导致新事件发生!
并帮自己一个忙:停止使用控件的VS默认名称!将控件命名为描述用户使用它们的名称会使代码更加可靠和可读!
Notice the bit at the bottom: if you just set the value of TextBox2.Text each time, it will immediately cause a new event to occur!
And do yourself a favour: stop using VS default names for controls! Calling your controls a name that describes what the user uses them for makes your code much more reliable and readable!
textBox2 => tbTaxDue
textBox10 => tbTaxRate
等。
and so forth.
用于财务计算我们 double
datataype并解决问题,从键入字母和其他特殊字符中取消它们。
for financial calculation us double
datataype and to resolve the problem restict them from typing alphabets and other special characters.
这篇关于C#.net中的税收计算错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文