根据复选框更改变量 [英] Change a variable based on checkboxes

查看:118
本文介绍了根据复选框更改变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个计算饮料价格的程序。我有三个复选框,每个复选框都有一个0.25美分的成分供客户选择添加。



如何为每个复选框的小计添加.25被选中了? (请记住,我需要在变量中保留额外金额,而不是仅为每个复选框添加.25。



我该怎么做用单选按钮?我还有3个单选按钮,每个按钮的数量都会改变小计。



谢谢。



我尝试了什么:



一个非常蹩脚的if / else语句将变量addOns更改为addOns = 0 + .25但它无法正常工作。

I have a program that calculates the price of a drink. I have three check boxes, each with a $.25 cent ingredient that the customer can choose to add.

How do I add .25 to the subtotal for each checkbox that is selected? (Keep in mind, I need to hold the "extras" amount in a variable rather than just tacking on .25 for each check box.

How do I do this with radio buttons? I also have 3 radio buttons, each with an amount that alters the subtotal.

Thanks.

What I have tried:

A really lame if/else statement that changed the variable "addOns" to "addOns = 0 + .25" but it didn't work correctly.

推荐答案

.25美分成分,客户可以选择添加。



如何将.25添加到所选的每个复选框的小计中?(请记住,我需要在变量中保留额外数量,而不是仅为.25复选框。



如何使用单选按钮执行此操作?我还有3个单选按钮,每个按钮的数量都会改变小计。



谢谢。



我尝试过什么:



一个非常蹩脚的if / else语句将变量addOns更改为addOns = 0 + .25但它不起作用正确。
.25 cent ingredient that the customer can choose to add.

How do I add .25 to the subtotal for each checkbox that is selected? (Keep in mind, I need to hold the "extras" amount in a variable rather than just tacking on .25 for each check box.

How do I do this with radio buttons? I also have 3 radio buttons, each with an amount that alters the subtotal.

Thanks.

What I have tried:

A really lame if/else statement that changed the variable "addOns" to "addOns = 0 + .25" but it didn't work correctly.


在同一容器(表单,面板等)中使用RadioButtons,其中只允许一个选项。使用CheckBoxes提供多个独立选择。



假设您有三个CheckBox:cbx1,cbx2,cbx3



0.定义变量,并为CheckStateChanged事件写一个EventHandler:
Use RadioButtons in the same container (Form, Panel, etc.) where only one choice is allowed. Use CheckBoxes to provide multiple independent choices.

Let's say you have three CheckBoxes: cbx1, cbx2, cbx3

0. define variables, and write an EventHandler for the CheckStateChanged event:
private decimal subtotal = 0.0m;
private CheckBox currentCbx;

private void Ingredients_CheckStateChanged(object sender, EventArgs e)
{
    currentCbx = sender as CheckBox;
    subtotal += currentCbx.Checked ? 0.25m : -0.25m;'

    // for testing
    textBox1.Text = subtotal.ToString();
}

1。在设计时将eventhandler连接到所有三个CheckBox,方法是选择它们,使用属性浏览器,或者在运行时的代码中。



使用这个技巧,你想要确保你的应用程序启动时CheckBoxes 全部未经检查



另一种方法,如果你不需要动态更新小计,就是简单地编写一个方法来根据CheckBoxes的状态来计算值。

1. wire-up the eventhandler to all three CheckBoxes at design time by selecting them all, and using the Property Browser, or, in code at run-time.

Using this technique, you want to be sure the CheckBoxes are all unchecked when your app starts.

Another method, if you don't need dynamic updating of the sub-total, is to simply write a method to calculate the value based on the state of thee CheckBoxes.


这篇关于根据复选框更改变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆