好的,我敢肯定这已经被问过了,但是我似乎找不到任何相关的东西。我正在尝试为学校创建作业。这是午餐订单菜单,您可以在其中下订单。我们必须计算小计,营业税和订单总额。我似乎无法找出要使用的正确代码,而且我不确定100%在这里尝试什么。
public partial class Form1 : Form
{
decimal subtotal = 0m;
decimal salesTax = 0m;
decimal orderTotal = 0m;
public Form1()
{
InitializeComponent();
rdoBurger.Checked = true;
rdoPizza.Checked = true;
rdoSalad.Checked = true;
}
private void btnExit_Click(object sender, EventArgs e)
{
Close();
}
private void clearTotals()
{
}
private void btnPlaceOrder_Click(object sender, EventArgs e)
{
if (sender is RadioButton)
{
clearTotals();
}
if (rdoBurger.Checked)
{
decimal subtotal = 6.95m;
subtotal = Convert.ToDecimal(lblSubtotal.Text);
}
这是我所拥有的,但未显示实际的小计,仍为空白。我在这里想念什么?
答案 0 :(得分:0)
这不是一个不好的开始。这更像是我期望看到的:
private void btnPlaceOrder_Click(object sender, EventArgs e)
{
// only ONE of these can be checked, so "else if" is used
if (rdoBurger.Checked)
{
subtotal = 6.95m;
}
else if (rdoPizza.Checked)
{
subtotal = 5.95m;
}
else if (rdoSalad.Checked)
{
subtotal = 4.95m;
}
// multiple of these could be checked, so only "if" is used
if (checkBox1.Checked)
{
subtotal = subtotal + 100.00m; // whatever this item costs
}
if (checkBox2.Checked)
{
subtotal = subtotal + 4.99m; // whatever this item costs
}
if (checkBox3.Checked)
{
subtotal = subtotal + 10.99m; // whatever this item costs
}
// compute the tax and the total:
salesTax = subtotal * 0.0775m;
orderTotal = subtotal + salesTax;
// output it to your labels/textboxes?
lblSubtotal.Text = "$" + subtotal.ToString("F2");
lblSalesTax.Text = "$" + salesTax.ToString("F2");
lblOrderTotal.Text = "$" + orderTotal.ToString("F2");
}
答案 1 :(得分:0)
单选按钮在容器中一次只能选择一个按钮。在这种情况下,似乎GroupBox是容器。如果您有几组单选按钮,则可以将GroupBox用作容器,并在每个GroupBox中选择一个单选按钮。因此,您不能将所有单选按钮的checked属性设置为true。
在您的btnPlaceOrder_Click
中,发件人不可能是单选按钮。发送者是被单击以运行事件代码的按钮。
private void button1_Click(object sender, EventArgs e)
{
//Find the radio button that is selected
RadioButton rButton = groupBox1.Controls.OfType<RadioButton>().FirstOrDefault(r => r.Checked == true);
switch (rButton.Text)
{
case "Hamburger - $6.95":
subTotal = 6.95m;
break;
case "Pizza - $5.95":
subTotal = 5.95m;
break;
case "Salad - $4.95":
subTotal = 4.95m;
break;
}
//Add code to handle Add-on items
//For example - The first check box is "Add Onions" - $0.50
if (checkBox1.Checked)
subTotal += .5m;
lblSubTotal.Text = subTotal.ToString();
decimal tax = subTotal * .0775m;
lblTax.Text = tax.ToString();
decimal total = subTotal + tax;
lblTotal.Text = total.ToString();
}