0

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.

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

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)

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.

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

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.

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.

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

Community
  • 1
  • 1
Dr.Viper
  • 411
  • 2
  • 5
  • 17

2 Answers2

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

  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

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

Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
  • This makes sense. I am extremely grateful for your input. The situation is that, there must be one selection from each group box, and I need to know each selection. For this, will adding a unique int to each help?Because otherwise, the selection doesn't help me narrow down to the individual radiobutton. – Dr.Viper Jul 20 '16 at 08:53
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.

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

    public MyClass()
    {

    }
}

Or just use:

value = (int)myCheckBox.Tag;
tim
  • 114
  • 1
  • Thanks for your response. It is much helpful. I understand the problem with the multiple collections, but my choice was for a cosmetic reason of clubbing similar options together. Let me try otherwise. – Dr.Viper Jul 20 '16 at 08:52