因此,我有一个发票总额表格,其中应计算折扣率。我在代码中添加了第二个表单,该表单允许您更改营业税。我将其填充到表单中,并且实际上没有任何错误,但是我无法将其从frmSalesTax上的文本框中移动到{ {1}}在frmInvoiceTotal中。
frmInvoice总代码:
txtSalesTax.Text
frmSalesTax代码:
public frmInvoiceTotal()
{
InitializeComponent();
}
frmSalesTax percent = new frmSalesTax();
private void btnCalculate_Click(object sender, EventArgs e)
{
decimal productTotal = Convert.ToDecimal(txtProductTotal.Text);
decimal salesTax = (7.75m/100m) * productTotal;
decimal discountPercent = .0m;
if (productTotal < 100)
discountPercent = .0m;
else if (productTotal >= 100 && productTotal < 250)
discountPercent = .1m;
else if (productTotal >= 250)
discountPercent = .25m;
decimal discountAmount = (productTotal + salesTax) * discountPercent;
decimal subtotal = productTotal - discountAmount;
decimal invoiceTotal = (subtotal + salesTax) - discountAmount;
txtSubtotal.Text = subtotal.ToString("c");
txtSalesTax.Text = salesTax.ToString("c");
txtDiscountPercent.Text = discountPercent.ToString("p1");
txtDiscountAmount.Text = discountAmount.ToString("c");
txtTotal.Text = invoiceTotal.ToString("c");
txtProductTotal.Focus();
}
private void btnChange_Click(object sender, EventArgs e)
{
percent.salesTax = txtSalesTax.Text;
switch (percent.ShowDialog())
{
case DialogResult.OK:
txtSalesTax.Text = percent.salesTax;
break;
}
}
private void btnExit_Click(object sender, EventArgs e)
{
this.Close();
}
我知道我想念什么,但我不知道是什么。
答案 0 :(得分:1)
您有正确的想法在frmSalesTax
上建立一个属性以进行通信...但是您并没有真正使用它。
在您的frmInvoiceTotal
中,您需要将当前值发送到frmSalesTax.salesTax
,然后处理返回DialogResult.OK
的百分比对话框的结果:
private void btnChange_Click(object sender, EventArgs e)
{
percent.salesTax = txtSalesTax.Text; //--> send current value to frmSalesTax
switch ( percent.ShowDialog() ) //--> ShowDialog will return the DialogResult of the pressed button
{
case DialogResult.OK:
txtSalesTax.Text = percent.salesTax; //--> update with new value from frmSalesTax
break;
}
}
...,并且在用户单击“确定”按钮时,需要在frmSalesTax
中将txtPercent.Text
放入salesTax
属性中。
private void btnOk_Click(object sender, EventArgs e)
{
this.salesTax = txtPercent.Text; //--> frmInvoiceTotal will read this after the OK button is clicked
txtPercent.Text = "";
Hide();
}
重要:您必须确保frmSalesTax
按钮设置了DialogResult
,以便frmInvoiceTotal.btnOk_Click
知道可以获取该值:< / p>
修改
(frmSalesTax
中的属性)不必基于表单的文本值...因为在表单隐藏时将其设置为“”。这就是您想要的属性:
public string salesTax
{
get;
set;
}
这将与我前面提到的其他更改保持一致。
编辑2
很容易感到沮丧。有很多动人的东西,我可以理解眼睛如何交叉。这是问题的症结所在-您的计算正在浪费您的东西;-)
btnCalculate_Click
中的这些行:
decimal salesTax = (7.75m/100m) * productTotal;
decimal discountPercent = .0m;
//...
txtSalesTax.Text = salesTax.ToString("c");
txtDiscountPercent.Text = discountPercent.ToString("p1");
...应为初始值,并包含在表单的初始化代码中:
public frmInvoiceTotal()
{
InitializeComponent();
decimal salesTax = (7.75m/100m) * productTotal;
decimal discountPercent = .0m;
txtSalesTax.Text = salesTax.ToString("c"); //--> the initial value
txtDiscountPercent.Text = discountPercent.ToString("p1");
}
...然后,应该不重新填充txtSalesTax.Text
或txtDiscountPercent.Text
。 txtSalesTax.Text可能会从显示frmSalesTax
开始更新,我想您可能会制作另一种形式来覆盖折扣百分比。
private void btnCalculate_Click(object sender, EventArgs e)
{
decimal productTotal = Convert.ToDecimal(txtProductTotal.Text);
decimal salesTax = Convert.ToDecimal(salesTax.Text) * productTotal; //--> if it got changed in frmSalesTax
decimal discountPercent = .0m;
if (productTotal < 100)
discountPercent = .0m;
else if (productTotal >= 100 && productTotal < 250)
discountPercent = .1m;
else if (productTotal >= 250)
discountPercent = .25m;
decimal discountAmount = (productTotal + salesTax) * discountPercent;
decimal subtotal = productTotal - discountAmount;
decimal invoiceTotal = (subtotal + salesTax) - discountAmount;
txtSubtotal.Text = subtotal.ToString("c");
//txtSalesTax.Text = salesTax.ToString("c"); //--> don't do this...it steps on what came from frmSalesTax
//txtDiscountPercent.Text = discountPercent.ToString("p1"); //--> when you add another form to override this
txtDiscountAmount.Text = discountAmount.ToString("c");
txtTotal.Text = invoiceTotal.ToString("c");
txtProductTotal.Focus();
}
我敢打赌,这会让您更加接近:-)
答案 1 :(得分:0)
像这样更改代码:
private void btnChange_Click(object sender, EventArgs e)
{
percent.salesTax = txtSalesTax.text;
percent.ShowDialog();
}