C# - 如何分别从每个单选 GroupBox 返回 int 值? [英] C# - How do I separately return int values from each radio GroupBox?

查看:28
本文介绍了C# - 如何分别从每个单选 GroupBox 返回 int 值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请允许我澄清我的情况和方法.

Please allow me to clarify my situation and approach.

我有一个 10 列的 excel 文件.前五列包含构成查询条件的数据,而后五列包含构成结果的数据.

我正在尝试使用数据库或excel中的这些数据制作软件(如果数据库很困难)前五列将被链接到哪里到列表或菜单或目前五个不同组中的 RadioButtons.当做出选择并按下提交按钮时,结果将显示为来自 excel 中匹配行的五个数据或数据库.

I am trying to make a software with this data in a database or excel (if database is difficult) where the first five columns will be linked to a List or Menu or presently RadioButtons in five different groups. When the selections are made and SUBMIT button is pressed, the result will be displayed as five data from matching rows in the excel or database.

这就是我想要达到的目标.

That is what I am trying to achieve.

如果我的方法有误,请评论.在此先感谢所有帖子.

Please comment if my approach is wrong. Thanks in advance to all the posts.

---------------------------原始问题----------------------------

--------------------------Original Question----------------------------

我在 WPF 上有 5 个分组框(1 个有 5 个单选按钮,2 个有 3 个,3 个和 4 个有 11 个,5 个有 4 个).根据从每个 groupBox 中选择哪个单选按钮,该软件预计会给出四个参数输出(来自 CSV 或数据库)

I have 5 groupboxes(1 has 5 radiobuttons, 2 has 3, 3 and 4 has 11, and 5 has 4 ) on a WPF. Based on which radiobutton is selected from each groupBox, the software is expected to give four parameters output (from a CSV or a database)

我用过 怎么做我从分组框中选中了哪个单选按钮? King King 给出的代码,并修改它以用于我的目的.

I have used How do I get which radio button is checked from a groupbox? the code from this given by King King, and modified it to use for my purpose.

var checkedRadio = new []{groupBox1, groupBox2, groupBox3, groupBox4, groupBox5}.SelectMany(g=>g.Controls.OfType<RadioButton>().Where(r=>r.Checked));
foreach(var c in checkedRadio)
    MessageBox.Show(c.Name);

这给我的是一个又一个被选中作为消息框的单选按钮.

What this gives me is the radiobuttons selected one after another as message box.

我要感谢 King King 以及其他许多人(超过 20 个),他们对各种答案的回答帮助我达到了这一点.

I want to thank King King, and the many others (more than 20) whose answers for various answers helped me reach this point.

我遇到的问题是如何使用 foreach 循环将值分配给五个单独的 int,然后使用它来计算最终的 int 值,我可以将其分配给我想要显示的各种结果.

Where I am stuck is how to use the foreach loop to assign values to five separate int and then use it to calculate a final int value which I can assign to various results I want to display.

我尝试在每个 CheckedChanged 事件处理程序中为 int 赋值,但它们都是私有的 void 并且不返回任何内容.我试图将它们更改为返回整数,但我似乎无法破解那个坚果.

I tried to assign values to int in each CheckedChanged event handler but all of them are private void and not returning anything. I tried to change them to return ints but I can't seem to crack that nut.

我确信我的整个方法在这里可能是错误的,但我对此没有太多想法.

I am sure my whole approach could be wrong here, but I have not much idea in this.

//我基本上想要的是在g=5组中给出选项,并根据这5组(excel中的列)的选择,显示数据库中匹配行中4个对应列的结果.我目前的上述方法是一种解决方法,因为我不知道使用数据库.//

//What I basically want is to give options in g=five groups, and based on selections from these five groups (columns in an excel), display results from 4 corresponding columns in the matching row in a database. My present approach above is a workaround since I have no knowledge of working with databases.//

推荐答案

我认为你应该省略将值赋值给 五个分隔 ints,但计算结果值.例如,让我们按位对 groupBoxs 进行编码,并计算一个 位掩码

I think you should omit assigning values to five separated ints, but compute the resulting value. For instance, let's encode groupBoxs by bits, and compute a bit mask

  groupBox1 - 00001 (let it be checked)
  groupBox2 - 00010   
  groupBox3 - 00100 (let it be checked)   
  groupBox4 - 01000 (let it be checked)  
  groupBox5 - 10000
  -----------------
  mask        01101 (13)

因此,如果groupBox1groupBox3groupBox4检查,最终掩码将为01101

so if groupBox1, groupBox3 and groupBox4 are checked the final mask will be 01101

var result = new [] {groupBox1, groupBox2, groupBox3, groupBox4, groupBox5}
  .Select((box, index) => new {
    box = box,
    mask = 1 << index
  }) 
  .Where(item => item.box.Controls
    .OfType<RadioButton>()
    .Any(button => button.Checked))
  .Sum(item => item.mask);

如果您必须使用任意权重,请将它们组织成一个集合,例如数组

In case you have to use arbitrary weights organize them into a collection e.g., array

  double[] weights = new double[] {1.2, 3.58, 4.62, -1.7, 0.9};

  double result = new [] {groupBox1, groupBox2, groupBox3, groupBox4, groupBox5}
    .Select((box, index) => new {
       box = box,
       weight = weights[index] 
     }  
    .Where(item => item.box.Controls
       .OfType<RadioButton>()
       .Any(button => button.Checked))
    .Sum(item => item.weight);

指导原则保持不变:不要使用分隔值:因为您有一个集合用于控件(组框),请将相应的值放入集合中 还有

The guiding principle remains the same: do not use separated values: since you have a collection for controls (groupboxes), put corresponding values into a collection as well

这篇关于C# - 如何分别从每个单选 GroupBox 返回 int 值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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