c# - 不使用点击事件 C# 计算总数

标签 c# winforms calculator

我编写了一个 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 自动计算?

最佳答案

    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 方法。

关于c# - 不使用点击事件 C# 计算总数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25161287/

相关文章:

javascript - 创建利润计算器

c# - 来自图像的 Android org.webrtc.VideoRenderer.I420Frame 数组

c# - 获取 HttpRequest 完整的请求字符串

c# - Azure网站使用asp.net core进行远程调试: Symbol not loading

c# - Convert.FromBase64String 有时返回 unicode,或 UTF-8

c# - 如何根据整数变量的值动态创建多个标签和文本框?

c# - Listview后台绘制问题C# Winform

c# - 使用 C# 创建带有自定义按钮的消息框

c# - 如何添加计算器开始菜单搜索?

java - 使用 JFrame 的 JAVA 贷款计算器出现异常?