Here is the loop I have so far
foreach (CheckBox chk in gpbSchedule.Controls.OfType<CheckBox>())
{
if (chk.Checked)
{
//Code goes here
}
}
The checkboxes all have text values of the days of the week. Monday, Tuesday ect.
I want the end result to be one string that looks like "Monday, Tuesday and Friday" depending on the whether the box is check.
The loop will change a bool so a know whether at least one checkbox is checked. This will be used in a if statement after where the produced string will be displayed so if none are checked no string will be displayed. I think this means it doesn't matter what the string looks like to start off with if that helps.
I hope I've been clear. If you need more detail please ask.
Thank you in advance.
Current code:
string days = "*";
foreach (CheckBox chk in gpbSchedule.Controls.OfType<CheckBox>())
{
if (chk.Checked)
{
days += "#" + chk.Text;
}
}
days = days.Insert(days.LastIndexOf('#'), " and ");
days = days.Remove(days.LastIndexOf('#'), 1);
days = days.Replace("#", ", ");
days = days.Replace("* and ", "");
days = days.Replace("*, ", "");
Can anyone see anything wrong with this?