Resolved transpose data from checkbox to another form

jhcoelho

Member
Joined
Mar 14, 2023
Messages
6
Programming Experience
Beginner
I have 6 checkboxes, limited to a maximum of 3 choices, now I want to transfer these choices to a new form. if i use:
//Session["hobbies1"] = cbx1.Text, 2,3,...,
passes all the checkboxes, but I only want the ones that were selected. how to make?
 
Solution
Assuming it is WinForms - that's what I provided a solution for in your previous thread - then you can get the check controls using LINQ. If you have hardcoded an array, which I did in my previous example, then you can get them from that, e.g.
C#:
var checkedBoxes = checkBoxes.Where(cb => cb.Checked);
If you have isolated the controls with a Panel then you can get them from that, e.g.
C#:
var checkedBoxes = myPanel.Controls.OfType<CheckBox>().Where(cb => cb.Checked);
You can then get the Text from each one and, if required, concatenate those values:
C#:
var checkedBoxesText = string.Join(", ",
                                   checkBoxes.Where(cb => cb.Checked)
                                             .Select(cb =>...
You've posted this in the Windows Forms forum but you appear to be trying to use a session variable in your code, which would imply ASP.NET of some sort. Please clarify.
 
Assuming it is WinForms - that's what I provided a solution for in your previous thread - then you can get the check controls using LINQ. If you have hardcoded an array, which I did in my previous example, then you can get them from that, e.g.
C#:
var checkedBoxes = checkBoxes.Where(cb => cb.Checked);
If you have isolated the controls with a Panel then you can get them from that, e.g.
C#:
var checkedBoxes = myPanel.Controls.OfType<CheckBox>().Where(cb => cb.Checked);
You can then get the Text from each one and, if required, concatenate those values:
C#:
var checkedBoxesText = string.Join(", ",
                                   checkBoxes.Where(cb => cb.Checked)
                                             .Select(cb => cb.Text));
 
Solution
Assuming it is WinForms - that's what I provided a solution for in your previous thread - then you can get the check controls using LINQ. If you have hardcoded an array, which I did in my previous example, then you can get them from that, e.g.
C#:
var checkedBoxes = checkBoxes.Where(cb => cb.Checked);
If you have isolated the controls with a Panel then you can get them from that, e.g.
C#:
var checkedBoxes = myPanel.Controls.OfType<CheckBox>().Where(cb => cb.Checked);
You can then get the Text from each one and, if required, concatenate those values:
C#:
var checkedBoxesText = string.Join(", ",
                                   checkBoxes.Where(cb => cb.Checked)
                                             .Select(cb => cb.Text));

solved, thanks
 
If your issue is resolved, please mark the thread Resolved from the menu above the first post. If a post provided the solution, please mark that post as the solution. I have done both for you in this case.
 
Back
Top Bottom