想知道如何轻松从复选框中添加总和

发布于 2025-01-22 10:41:07 字数 496 浏览 0 评论 0原文

我是C#的新手,所以我在以下问题上遇到了一些困难。

以下是我以下代码,

int access = 1;
int onlineVideos = 2;
int personalTrainer = 20;
int dietConsultation = 20;

int extras = 0;

if (checkBox1.Checked)
{
    extras += access;
}
else if (checkBox2.Checked)
{
    extras += onlineVideos;
}
else if (checkBox3.Checked)
{
    extras += personalTrainer;
}
else if (checkBox4.Checked)
{
    extras += dietConsultation;
}

textBox6.Text = extras.ToString();

我希望能够轻松地添加所检查的所有框的总和,然后将其打印到文本框中。

im extremely new to c# so im having a little difficulty with the following problem.

below is my following code

int access = 1;
int onlineVideos = 2;
int personalTrainer = 20;
int dietConsultation = 20;

int extras = 0;

if (checkBox1.Checked)
{
    extras += access;
}
else if (checkBox2.Checked)
{
    extras += onlineVideos;
}
else if (checkBox3.Checked)
{
    extras += personalTrainer;
}
else if (checkBox4.Checked)
{
    extras += dietConsultation;
}

textBox6.Text = extras.ToString();

i want to be able to easily add the sums of all the boxes that are checked, and then print them to a textbox.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

乄_柒ぐ汐 2025-01-29 10:41:07

根据您的描述,您不需要多个if/否则子句,而只需总和检查状态的所有复选框的所有值。类似的内容:

foreach (Control c in this.Controls)
{
    if((c is CheckBox) && ((CheckBox) c).Checked)
      {
        yourdecimal += Convert.ToDecimal(c.Text); 
      }                
}

确切的方法取决于您的确切代码,因此可能需要更改“ This.controls”为例。否则可能有必要添加尝试/捕获以防止问题等问题等。这只是做到这一点的一种方法。如果您需要进一步的帮助,请添加您的源代码。

According to your description, you don't need multiple if/else clauses, but only a method which sums all values of your check boxes whose state is checked. Something like this:

foreach (Control c in this.Controls)
{
    if((c is CheckBox) && ((CheckBox) c).Checked)
      {
        yourdecimal += Convert.ToDecimal(c.Text); 
      }                
}

The exact method depends on your exact code, so it might be required to change the "this.Controls" as example. Or it might be necessary to add try/catch to prevent issues if there are non numeric values etc. This is just a way to do this. If you need further assistance, you should please add your source code.

南烟 2025-01-29 10:41:07

您可以借助三元运营商的帮助来简化代码:

int extras = (checkBox1.Checked ? 0 : access) + 
             (checkBox2.Checked ? 0 : onLineVideos) + 
             (checkBox3.Checked ? 0 : personalTrainer) + 
             (checkBox4.Checked ? 0 : dietConsultation);

一个更好的选择是 ext data(选项价格)和逻辑:

using System.Linq;

...

// Data: option and its price
var prices = new (CheckBox option, int price)[] {
  (checkBox1,  1),
  (checkBox2,  2),
  (checkBox3, 20),
  (checkBox4, 20),
};

...

// Logic: we sum all checked options
var extras = prices.Sum(item => item.option.Checked ? item.price : 0);

You can simplify the code a bit with a help of ternary operators:

int extras = (checkBox1.Checked ? 0 : access) + 
             (checkBox2.Checked ? 0 : onLineVideos) + 
             (checkBox3.Checked ? 0 : personalTrainer) + 
             (checkBox4.Checked ? 0 : dietConsultation);

A better option is to separate data (options' prices) and logic:

using System.Linq;

...

// Data: option and its price
var prices = new (CheckBox option, int price)[] {
  (checkBox1,  1),
  (checkBox2,  2),
  (checkBox3, 20),
  (checkBox4, 20),
};

...

// Logic: we sum all checked options
var extras = prices.Sum(item => item.option.Checked ? item.price : 0);
屋顶上的小猫咪 2025-01-29 10:41:07

作为 @jonas-metzler的解决方案的替代方法,您可以在复选框及其值之间创建一种映射表(字典)。如果您使用text复选框上使用属性的属性,则可以使用此功能,也可以用作其他问题的更通用解决方案。

var checkboxValues = new Dictionary<Checkbox, int>
{
    { checkBox1, access },
    { checkBox2, onlineVideos },
    { checkBox3, personalTrainer },
    { checkBox4, dietConsultation },
};

var sum = checkboxValues.Where(kvPair => kvPair.Key.Checked)
    .Sum(kvPair => kvPair.Value);

As an alternative to the solution by @jonas-metzler, you can create a sort-of mapping table (Dictionary) between checkboxes and their values. This can be used if you're using Text property on the Checkbox for something else or as a more general-purpose solution for similar problems outside WinForms.

var checkboxValues = new Dictionary<Checkbox, int>
{
    { checkBox1, access },
    { checkBox2, onlineVideos },
    { checkBox3, personalTrainer },
    { checkBox4, dietConsultation },
};

var sum = checkboxValues.Where(kvPair => kvPair.Key.Checked)
    .Sum(kvPair => kvPair.Value);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文