【发布时间】: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