C# 如何即时计算文本框中的两个数字,并在删除数字时更改结果?

C# 如何即时计算文本框中的两个数字,并在删除数字时更改结果?,c#,C#,当我从任何文本框中删除数字时,如何立即计算文本框中的两个数字并改变结果?? 这是我的密码 private void expenses_KeyDown(object sender, KeyEventArgs e) { if (expenses.Text != string.Empty) { decimal Pprice = Convert.ToDecimal(buyTotal.Text); de

当我从任何文本框中删除数字时,如何立即计算文本框中的两个数字并改变结果?? 这是我的密码

        private void expenses_KeyDown(object sender, KeyEventArgs e)
    {
        if (expenses.Text != string.Empty)
        {
            decimal Pprice = Convert.ToDecimal(buyTotal.Text);
            decimal expens = Convert.ToDecimal(expenses.Text);
            decimal final = Pprice + expens;
            buyTotal.Text = final.ToString();
        }
    }

但是,当我使用退格键并从任何文本框中删除数字,然后写下另一个数字时,我收到了错误的结果,我想现在很明显,当我在费用中键入数字时,我需要将其添加到buyTotal中。当我键入数字时,计算结果时,下面的代码应足以计算两个数字之和:

private void expenses_KeyDown(object sender, KeyEventArgs e)
{
    try
    {
        if (expenses.Text != string.Empty)
        {
            double Pprice = Convert.ToDouble(buyTotal.Text);
            double expens = Convert.ToDouble(expenses.Text);
            double final = Pprice + expens;
            buyTotal.Text = final.ToString("0.00");
        }
        else
        {
            buyTotal.Text="0.00";
        }
    }
    catch(Exception exc)
    {
        buyTotal.Text="0.00";
    }
}

以下代码应足以计算两个数字之和:

private void expenses_KeyDown(object sender, KeyEventArgs e)
{
    try
    {
        if (expenses.Text != string.Empty)
        {
            double Pprice = Convert.ToDouble(buyTotal.Text);
            double expens = Convert.ToDouble(expenses.Text);
            double final = Pprice + expens;
            buyTotal.Text = final.ToString("0.00");
        }
        else
        {
            buyTotal.Text="0.00";
        }
    }
    catch(Exception exc)
    {
        buyTotal.Text="0.00";
    }
}

请使用
KeyUp
而不是
KeyDown
,因为您的输入值在
KeyDown
事件之后写入
textbox

请检查以下内容:

private void expenses_KeyUp(object sender, KeyEventArgs e)
{
    if (expenses.Text != string.Empty)
    {
        decimal Pprice = Convert.ToDecimal(buyTotal.Text);
        decimal expens = Convert.ToDecimal(expenses.Text);
        decimal final = Pprice + expens;
        buyTotal.Text = final.ToString();
    }
    if (e.KeyCode == Keys.Back)
    {
        buyTotal.Text = (from DataGridViewRow row in dataGridView1.Rows where row.Cells[6].FormattedValue.ToString() != string.Empty select Convert.ToDecimal(row.Cells[6].FormattedValue)).Sum().ToString();
    }
}

请使用
KeyUp
而不是
KeyDown
,因为您的输入值在
KeyDown
事件之后写入
textbox

请检查以下内容:

private void expenses_KeyUp(object sender, KeyEventArgs e)
{
    if (expenses.Text != string.Empty)
    {
        decimal Pprice = Convert.ToDecimal(buyTotal.Text);
        decimal expens = Convert.ToDecimal(expenses.Text);
        decimal final = Pprice + expens;
        buyTotal.Text = final.ToString();
    }
    if (e.KeyCode == Keys.Back)
    {
        buyTotal.Text = (from DataGridViewRow row in dataGridView1.Rows where row.Cells[6].FormattedValue.ToString() != string.Empty select Convert.ToDecimal(row.Cells[6].FormattedValue)).Sum().ToString();
    }
}

由于您得到的输出不正确,请指定哪个输出错误,它的值是什么,以及哪些输入有用。要以书面形式回答这个问题将非常困难。因为您得到的输出不正确,请指定哪个输出错误,它的值是什么,以及哪些输入有用。要以书面形式回答这个问题将非常困难。