using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace WindowsFormsApplication2 { public partial class Form1 : Form { List<string> Perebor(GroupBox groupBox) { List<string> value = new List<string>(); foreach (Control control in groupBox.Controls) { if (control.GetType() == typeof(CheckBox)) { CheckBox checkBox = (CheckBox)control; if (checkBox.Checked == true) value.Add(checkBox.Text); } else if (control.GetType() == typeof(RadioButton)) { RadioButton radioButton = (RadioButton)control; if (radioButton.Checked == true) value.Add(radioButton.Text); } } return value; } public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { foreach (string text in Perebor(groupBox1)) { textBox1.Text = text; } } } } 

    1 answer 1

     textBox1.Text = text; 

    In this line, you completely overwrite the value in order not to lose what is already there - use concatenation, for example, like this:

     textBox1.Text += text + ";"; 

    note, I also added a delimiter entry (semicolon), you can remove it or use any other delimiter (a newline may suit you better)

    Well, keep in mind that access to controls is rather slow, so it makes sense to assemble the string into a local variable, and put the value into the control property once at the very end:

     string result = ""; foreach (string text in Perebor(groupBox1)) result += text + ";"; textBox1.Text = result; 

    And, of course, the standard recommendation is to use StringBuilder , not string , for string concatenation.

    • Many thanks. Everything works :) - Dmitrii Gonta