gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 12:59:31 25 4
gpt4 key购买 nike

我编写了一个 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com