Selected checkbox from checkbox group in .C # .net

Problem encountered in C # .NET, Visual Studio, Windows Form Application

I have a set of checkboxes placed randomly in one form and in one panel. So, if any checkbox selected on the form, its value should be added up.

Bottomline: Instead of using a lot of "If-else loops" to evaluate whether it has been checked or not. I want to simplify it using a "loop cycle".

Is there a function like Checkbox group name that I can use?

I want to write something like this:

 for(int i=0;i<checkboxes.length;i++) { string str; if(chkbox.checked) { str+=chkbox.value; } } 

Where checkboxes is the name of the group.

+4
source share
5 answers

You can use a simple LINQ query

 var checked_boxes = yourControl.Controls.OfType<CheckBox>().Where(c => c.Checked); 

where yourControl is the control containing your checkboxes.


checked_boxes now an object that implements the IEnumerable<CheckBox> that represents the request. Usually you want to foreach over this query using the foreach :

 foreach(CheckBox cbx in checked_boxes) { } 

You can also convert this query to a list ( List<Checkbox> ) by calling .ToList() , either on checked_boxes, or immediately after Where(...) .


Since you want to combine the Text checkboxes into one string, you can use String.Join .

 var checked_texts = yourControl.Controls.OfType<CheckBox>() .Where(c => c.Checked) .OrderBy(c => c.Text) .Select(c => c.Text); var allCheckedAsString = String.Join("", checked_texts); 

I also added an OrderBy clause to make sure the checkboxes are sorted by their Text .

+6
source
 CheckBox[] box = new CheckBox[4]; box[0] = checkBox1; box[1] = checkBox2; box[2] = checkBox3; box[3] = checkBox4; for(int i=0; i<box.length; i++) { string str; if(box[i].checked== true) { str += i.value; } } 

I think this code will work with DotNet4.0. Plz will inform me that some error has occurred. Treat box as a regular array.

+1
source

Check all the checkboxes for one CheckedChanged event handler and create a line if the checkbox is checked or unchecked. In the following query, a line will be constructed containing the names of all the flags with form validation:

 private void Checkbox_CheckedChanged(object sender, EventArgs e) { // this will use all checkboxes on Form string str = Controls.OfType<CheckBox>() .Where(ch => ch.Checked) .Aggregate(new StringBuilder(), (sb, ch) => sb.Append(ch.Name), sb => sb.ToString()); // use string } 
0
source

If all the checkboxes are in the group box, you can do this:

 foreach(Control c in myGroupBox.Controls) { if (c is CheckBox) { //do something CheckBox temp = (CheckBox)c; if(temp.Checked) //its checked } } 
0
source

I believe that besides subscribing to the CheckedChanged event, there is no alternative, even if it is contained in any panel or form, you should use if else,

if it were a web base, such as asp.net or php, we could use jquery, because it gives us the ability to scroll through each specific event using .each and get its value

0
source

All Articles