C# 十进制值表示错误

C# 十进制值表示错误,c#,decimal,C#,Decimal,我对十进制输入有一个问题,下面是我在点击按钮时使用的代码 private void button6_Click_1(object sender, EventArgs e) { string PName = "كريب دجاج شاورما"; string PPrice = "20.50"; string PQty = "1"; textBox1.Text = PName; textBox6.Text =

我对十进制输入有一个问题,下面是我在点击按钮时使用的代码

private void button6_Click_1(object sender, EventArgs e)
    {
        string PName = "كريب دجاج شاورما";
        string PPrice = "20.50";
        string PQty = "1";

        textBox1.Text = PName;
        textBox6.Text = PPrice;
        textBox2.Text = PQty;
        textBox5.Text = "0";
    }

    private void button7_Click_1(object sender, EventArgs e)
    {
        string PName = "كريب تشيكن شريمبو";
        string PPrice = "28";
        string PQty = "1";

        textBox1.Text = PName;
        textBox6.Text = PPrice;
        textBox2.Text = PQty;
        textBox5.Text = "0";
    }
单击时带有PPrice 20.50的图标会在文本框6中显示无效值 当点击第二个带有PPrice 28的按钮时,它将正常继续

我如何修正它,使它接受小数

更新

前面的代码不是问题所在,真正的问题在于这段代码,当计算不是在文本框本身上进行时,它会显示错误,所以这是完整的代码

private void button6_Click_1(object sender, EventArgs e)
    {
        string PName = "كريب دجاج شاورما";
        string PPrice = "20.50";
        string PQty = "1";

        textBox1.Text = PName;
        textBox6.Text = PPrice;
        textBox2.Text = PQty;
        textBox5.Text = "0";
    }

    private void button7_Click_1(object sender, EventArgs e)
    {
        string PName = "كريب تشيكن شريمبو";
        string PPrice = "28";
        string PQty = "1";

        textBox1.Text = PName;
        textBox6.Text = PPrice;
        textBox2.Text = PQty;
        textBox5.Text = "0";
    } private void textBox3_TextChanged(object sender, EventArgs e)
    {

        Multiply();
    }

    private void textBox6_TextChanged(object sender, EventArgs e)
    {
        int first = 0;
        int second = 0;
        if (Int32.TryParse(textBox5.Text, out second) && Int32.TryParse(textBox6.Text, out first))
            textBox3.Text = (first + second).ToString();
    }

    private void textBox5_TextChanged(object sender, EventArgs e)
    {
        int first = 0;
        int second = 0;
        if (Int32.TryParse(textBox5.Text, out second) && Int32.TryParse(textBox6.Text, out first))
            textBox3.Text = (first + second).ToString();
    }

出现错误是因为您试图将带小数点的字符串转换为整数

您有以下代码:

string PPrice = "20.50";
textBox6.Text = PPrice;
int first = 0;
int second = 0;
if (Int32.TryParse(textBox5.Text, out second) && Int32.TryParse(textBox6.Text, out first))
            textBox3.Text = (first + second).ToString();
那么您就有了这个代码:

string PPrice = "20.50";
textBox6.Text = PPrice;
int first = 0;
int second = 0;
if (Int32.TryParse(textBox5.Text, out second) && Int32.TryParse(textBox6.Text, out first))
            textBox3.Text = (first + second).ToString();
Int.TryParse(textbox6.Text,out first
失败并返回一个
false
,因为
20.50
无法转换为
整数

您需要将值解析为十进制,如果成功,则继续:

decimal pPrice;

if (decimal.TryParse(textbox6.Text, out pPrice))
{
    // do what you need 
}
else
{
}

出现错误是因为您试图将带小数点的字符串转换为整数

您有以下代码:

string PPrice = "20.50";
textBox6.Text = PPrice;
int first = 0;
int second = 0;
if (Int32.TryParse(textBox5.Text, out second) && Int32.TryParse(textBox6.Text, out first))
            textBox3.Text = (first + second).ToString();
那么您就有了这个代码:

string PPrice = "20.50";
textBox6.Text = PPrice;
int first = 0;
int second = 0;
if (Int32.TryParse(textBox5.Text, out second) && Int32.TryParse(textBox6.Text, out first))
            textBox3.Text = (first + second).ToString();
Int.TryParse(textbox6.Text,out first
失败并返回一个
false
,因为
20.50
无法转换为
整数

您需要将值解析为十进制,如果成功,则继续:

decimal pPrice;

if (decimal.TryParse(textbox6.Text, out pPrice))
{
    // do what you need 
}
else
{
}

textBox6是什么类型的?这与显示“小数”无关,因为在这两种情况下,分配给
.Text
的值都是字符串。请慢慢调试并真正查看是否调用了预期的函数。当您说“无效值”时你的意思是什么?好的,点击按钮,信息会进入文本框,除了小数点给出的“无效”,然后单击按钮将它们添加到listview,下面是我在调试mscorlib.dll中发生的“System.FormatException”类型的未处理异常时得到的信息其他信息:输入字符串的格式不正确。好的,我太笨了,这不是问题发生的地方,我现在将用出现问题的代码更新什么textBox6的类型是?这与显示“小数”无关,因为在这两种情况下,分配给
.Text
的值都是字符串。请慢慢调试,并真正查看是否调用了预期的函数。此外,当您说“无效值”时,您的意思是什么?单击按钮,信息将转到文本框,但小数框除外“无效",然后单击按钮将它们添加到listview,下面是我在调试mscorlib.dll中发生的“System.FormatException”类型的未处理异常时得到的信息其他信息:输入字符串的格式不正确。好的,我太笨了,这不是问题发生的地方,我现在将用出现问题的代码更新数据库s、 经过测试,效果很好,解决了我的问题谢谢,经过测试,效果很好,解决了我的问题