这是一个针对不同餐厅的小型应用程序,以及它们开放的一周中的哪几天。在我的界面中,我有一周中每个日期的复选框。我想在string.Format
每个选定的日期(选中)框中输出。这可能吗?
这是抓住他复选框的代码:
foreach (CheckBox cb in grpDaysOpen.Controls) // Add days of week that the restaurant is open
{
DayOfWeek enumConvertedSuccessfully; // Will contain good enums soon
if (cb.Checked && Enum.TryParse<DayOfWeek>(cb.Text, out enumConvertedSuccessfully))
favoriteDiningPlace.OpenDays.Add(enumConvertedSuccessfully);
}
以下是当前覆盖ToString
:
return string.Format(
"{0} is a chian of {1} with a seating capacity of {2}:\r\nSmoking is {3}. Last month's sales were {4}, while last months costs were {5}. \r\nThe restaurant is open {6}",
this.Name,
this.Chain,
this.SeatingCapacity,
this.Smoking,
this.LastMonthSales,
this.LastMonthCosts,
this.OpenDays);
我不知道是否需要包含任何其他代码。如果您需要澄清,请告诉我。目前上述ToString
输出
&#34; system.collections.generic.list` [System.DayOfWeek]&#34;
代表this.OpenDays
。
答案 0 :(得分:3)
List<DayOfWeek> d = new List<DayOfWeek> { DayOfWeek.Monday, DayOfWeek.Saturday };
string res = string.Join(", ", d);
答案 1 :(得分:1)
试试这个
string days = String.Join(", ",this.OpenDays.Select(x => x.ToString()));
答案 2 :(得分:0)
覆盖OpenDays的ToString(),如下所示
public class OpenDays
{
public override ToString()
{
StringBuilding formattedOpenDays = new StringBuilder();
//od is an object of type of single element in OpenDays collection
foreach(OpenDay od in OpenDays)
{
formattedOpenDays.Add(od.ToString());
}
return formattedOpenDays.ToString();
}
}