C# - 如何从每个单选框GroupBox单独返回int值?

时间:2022-04-17 13:42:23

Please allow me to clarify my situation and approach.

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

I have an excel file with 10 columns. First five columns have data which form the conditions for the query, while the last five columns contain data which form the result.

我有一个包含10列的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.

我正在尝试使用数据库或excel(如果数据库很难)制作软件,其中前五列将链接到列表或菜单或目前五个不同组中的RadioButtons。进行选择并按下SUBMIT按钮时,结果将显示为excel或数据库中匹配行的五个数据。

That is what I am trying to achieve.

这就是我想要实现的目标。

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

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

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

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

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)

我在WPF上有5个groupbox(1个有5个radiobuttons,2个有3个,3个有4个,5个有4个)。根据从每个groupBox中选择的radiobutton,软件应该提供四个参数输出(来自CSV或数据库)

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.

我用过如何从组合框中选中哪个单选按钮? King King给出的代码,并修改它以用于我的目的。

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.

这给我的是一个接一个地选择的无线电按钮作为消息框。

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

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

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.

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

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.

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

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

我相信我的整个方法在这里可能是错的,但我对此并不太了解。

//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.//

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

2 个解决方案

#1


0  

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

我认为您应该省略将值分配给五个单独的整数,但计算结果值。例如,让我们按位编码groupBox,并计算位掩码

  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)

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

因此,如果选中groupBox1,groupBox3和groupBox4,则最终掩码将为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

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

#2


0  

Sorry but that's ugly having separate collections. You should either make a user control based on the checkbox that has the int, or use the tag of the control. You could have the group box as a control that abstracts the calculations to the form but that would depend on what else you do with them.

对不起但是这个单独的收藏很难看。您应该根据具有int的复选框创建用户控件,或者使用控件的标记。您可以将组框作为控件,将计算抽象到表单,但这取决于您使用它们做什么。

public class MyClass : CheckBox 
{
    public int MyValue { get; set; }

    public MyClass()
    {

    }
}

Or just use:

或者只是使用:

value = (int)myCheckBox.Tag;

#1


0  

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

我认为您应该省略将值分配给五个单独的整数,但计算结果值。例如,让我们按位编码groupBox,并计算位掩码

  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)

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

因此,如果选中groupBox1,groupBox3和groupBox4,则最终掩码将为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

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

#2


0  

Sorry but that's ugly having separate collections. You should either make a user control based on the checkbox that has the int, or use the tag of the control. You could have the group box as a control that abstracts the calculations to the form but that would depend on what else you do with them.

对不起但是这个单独的收藏很难看。您应该根据具有int的复选框创建用户控件,或者使用控件的标记。您可以将组框作为控件,将计算抽象到表单,但这取决于您使用它们做什么。

public class MyClass : CheckBox 
{
    public int MyValue { get; set; }

    public MyClass()
    {

    }
}

Or just use:

或者只是使用:

value = (int)myCheckBox.Tag;