【问题标题】:Calculating a total without using a click event C#在不使用点击事件 C# 的情况下计算总数
【发布时间】:2014-09-29 10:36:12
【问题描述】:

我编写了一个 Windows 窗体应用程序计算器,它计算房屋特定部分的平方英尺并估算重建该结构的大致成本。

我正在使用 TextChanged 事件,以便在用户输入输入时将平方英尺转换为计算的重建成本。

public void txtBuiltInGarage_TextChanged(object sender, EventArgs e)
{
    int outputValue = 0;
    bool isNumber = false;

    isNumber = int.TryParse(txtBuiltInGarage.Text, out outputValue);

    if (!isNumber)
    {
        txtBuiltInGarage.Text = "";
        txtBuiltInGarageCost.Text = "";
    }
    else
    {
        int builtinGarageSQ;
        int builtinGarageCostPerSF = 100;

        builtinGarageSQ = int.Parse(txtBuiltInGarage.Text.ToString());
        builtinGarageCost = builtinGarageSQ * builtinGarageCostPerSF;
        txtBuiltInGarageCost.Text = builtinGarageCost.ToString();
    }
}

但是,我希望此功能能够延续到所有字段的总数。例如,当用户输入 100 平方英尺的内置车库时,它会在 txtBuiltInGarageCost 中显示 100 * 100 的总和,但它也会在总和框中显示总和,并将其与其他值字段进行核对以查看如果需要进行任何进一步的计算。基本上,这个应用程序是一个早期的 Excel 电子表格的转换,我想保持“总和”的感觉。使用单击事件的问题在于,由于单击按钮后某些字段仍可编辑,因此我必须编写一个我认为有损于编写良好程序的整体感觉的工作。这是用户单击计算按钮时我的代码的样子。

private void btnCalculate_Click(object sender, EventArgs e)
{
    totalRC = livingSpaceCost + builtinGarageCost + attachedGarageCost + deckCost + openPorchCost + enclosedPorchCost + additionalFeaturesCost;
    txtTotalReplacementCost.Text = totalRC.ToString();

    txtLivingSpace.ReadOnly = true;
    txtBuiltInGarage.ReadOnly = true;
    txtAttachedGarage.ReadOnly = true;
    txtDeck.ReadOnly = true;
    txtEnclosedPorch.ReadOnly = true;
    txtOpenPorch.ReadOnly = true;
    txtAdditionalFeaturesCost.ReadOnly = true;
    cmbConstructionQuality.Visible = false;
    lblConstructionQualityOutput.Visible = true;
    cmbConstructionType.Visible = false;
    lblConstructionTypeOutput.Visible = true;
    lblClearAlert.Visible = true;

    lblConstructionQualityOutput.Text = cmbConstructionQuality.SelectedItem.ToString();
    lblConstructionTypeOutput.Text = cmbConstructionType.SelectedItem.ToString();
}

那么,当我在其他字段中输入/更改值时,如何使 txtTotalReplacementCost 自动计算?

【问题讨论】:

  • 数据将total 值绑定到您希望更新的字段
  • WPF/windows 窗体?如果你使用 WPF,你可以在你想要的每个字段/文本框上使用绑定,每次更改(在设置方法上)你都将运行“ShowResults()”方法。
  • 对不起,总是忘记包含这个...我正在使用 Windows 窗体应用程序。
  • 所以如果你使用 Windows 窗体,嗯.. 我为你感到抱歉:)
  • WPF 的标签使用.Content,这就是您可以区分的方式。

标签: c# winforms calculator


【解决方案1】:
    public partial class Form1 : Form
        {
            public double livingSpaceCost;
            public double builtinGarageCost;
            public double attachedGarageCost;
            public double deckCost;
            public double openPorchCost;
            public double enclosedPorchCost;
            public double additionalFeaturesCost;
            public Form1()
            {
                InitializeComponent();
                livingSpaceCost = 0;
                builtinGarageCost = 0;
                attachedGarageCost = 0;
                deckCost = 0;
                openPorchCost = 0;
                enclosedPorchCost = 0;
                additionalFeaturesCost = 0;
            }
            void calctotalrc()
            {
                double total;
                total = livingSpaceCost + builtinGarageCost + attachedGarageCost + deckCost + openPorchCost + enclosedPorchCost + additionalFeaturesCost;
                txtTotalReplacementCost.Text = total.ToString();
            }

public void txtBuiltInGarage_TextChanged(object sender, EventArgs e)
    {
        int outputValue = 0;
        bool isNumber = false;

        isNumber = int.TryParse(txtBuiltInGarage.Text, out outputValue);

        if (!isNumber)
        {
            txtBuiltInGarage.Text = "";
            txtBuiltInGarageCost.Text = "";
        }
        else
        {
            int builtinGarageSQ;
            int builtinGarageCostPerSF = 100;

            builtinGarageSQ = int.Parse(txtBuiltInGarage.Text.ToString());
            builtinGarageCost = builtinGarageSQ * builtinGarageCostPerSF;
            txtBuiltInGarageCost.Text = builtinGarageCost.ToString();
        }
        calctotalrc();
    }
 }

在所有文本更改事件中调用 calctotalrc 方法。

【讨论】:

  • 结合 calctotalrc 方法可以解决这个问题。谢谢你的建议。
【解决方案2】:

您可以尝试在每次用户更改输入框时使用 Leave 事件触发对数据进行有效输入的测试。

Leave 可用于模拟 JavaScript onblur 事件。另请参阅此答案: How to mimic JavaScript's onBlur event in Windows Forms?

您还可以将“Enter”事件添加到文本框,以便在用户开始编辑计算所依赖的文本框之一时清除计算相关文本框。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-02-01
    • 1970-01-01
    • 2021-08-07
    • 2017-12-15
    • 2011-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多