在我的计划中,我有一个按钮,可以在接下来的14天内向我展示每个人(人员列表)的生日。它的工作原理如下:
主要形式:
private void buttonBirthdaysNext2Weeks_Click(object sender, EventArgs e)
{
int days = (int)numericUpDown1.Value;
string caption = string.Format("Birthdays in next {0} days", days);
string message = birthdays.UpComingBirthdays(days);
if (message == "")
{
message = "No birthdays found!";
}
MessageBox.Show(message, caption, MessageBoxButtons.OK,
MessageBoxIcon.Information);
}
使用被称为方法的类:
public string UpComingBirthdays(int days)
{
string names = "";
foreach (Person person in people)
{
if (person.HowManyDaysTillBirthday() <= 14)
{
int number = person.HowManyDaysTillBirthday();
//Debug.Print(number.ToString());
// Debug.print(int days2 = HowManDaysTillBirthday(););
if (names != "")
{
names = names + Environment.NewLine; // Each person name and details will be printed on new line
}
names = names + person.FirstName + " " + person.LastName + " " + person.DateOfBirth(); //all these details (first, last and dob) will be printed/given to names string
}
}
return names;
}
我正在尝试类似的东西。我想展示每个月的生日情况;而不是按钮点击事件,这将使用组合框完成。组合框每个月列出一次;用户选择月份;然后,消息框会显示该月的每个人/生日。那么与之前的代码非常相似。但是,我遇到了麻烦并遇到以下错误:FormatException未处理。
创建组合框后,每个月手动输入项目(集合)属性中的项目,这是我尝试过的:
public void comboBoxMonthsInTheYear_SelectedIndexChanged(object sender, EventArgs e)
{
int monthWanted = int.Parse(comboBoxMonthsInTheYear.SelectedItem.ToString());
string caption = string.Format("Birthdays in monthWanted", monthWanted);
string message = birthdays.birthdaysInMonth(monthWanted);
}
&安培;
public string birthdaysInMonth(int monthWanted)
{
string names = "";
foreach (Person person in people)
{
if (person.MonthBorn == monthWanted)
{
if (names != "")
{
names = names + Environment.NewLine; // Each person name and details will be printed on new line
}
names = names + person.FirstName + " " + person.LastName + " " + person.DateOfBirth(); //all these details (first, last and dob) will be printed/given to names string
}
}
return names;
}
}
单击组合框中的一个月后,将显示FormatException错误。我不确定从哪里开始。任何建议表示赞赏;如果没有,谢谢你的阅读!如果你觉得我的问题写得不好也不值得在这里发布(我承认这是非常基本的东西),我会事先道歉。