1

in my program, i have a button that shows me everyone's (a list of people's) birthdays in the next 14 days. it works as follows:

main form:

    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);
    }

class with the method that is called:

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;
}

i am attempting something similar. i want to show how birthdays for each given month; rather than a button click event, this would be done using a combobox. the combobox lists each month; the user selects the month; a messagebox then shows each person/birthday in that month. very similar to the previous code, then. yet, i am having trouble and encountering the following error: FormatException was unhandled.

after creating a combobox, and inputting each month manually as an item in its Items (collection) property, here's what i've tried:

    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;
}
}

upon clicking a month in the combobox, the FormatException error is presented. i'm unsure where to proceed from here. any advice is appreciated; if not, thank you for reading! i apologize in advance if you feel my question is neither well-written nor worthy of being posted here (it's very basic stuff, admittedly).

multipack1
  • 31
  • 1
  • 1
  • 6
  • Where does the StackTrace of the exception point to ? – D.J. Nov 01 '17 at 17:08
  • 1
    I believe you might be facing this error when the app starts, and exception is happening in `comboBoxMonthsInTheYear_SelectedIndexChanged` at this line `int monthWanted = int.Parse(comboBoxMonthsInTheYear.SelectedItem.ToString()); `. Is that so? – boop_the_snoot Nov 01 '17 at 17:11
  • Hi @o_O. I can confirm the exception error occurs at that line. – multipack1 Nov 01 '17 at 17:13
  • 1
    I'd imagine @o_O is correct. You probably want SelectedValue instead. Either way, you definitely want to null check it first. Also see https://stackoverflow.com/questions/2883481/combobox-selecteditem-vs-selectedvalue – Trioj Nov 01 '17 at 17:14
  • 2
    And maybe you should use Int.TryParse instead if Int.Parse – D.J. Nov 01 '17 at 17:16
  • 1
    Include this `If ((comboBoxMonthsInTheYear.SelectedIndex < 0) || comboBoxMonthsInTheYear.SelectedItem == null) return;` in the first line ie: *before* `int monthWanted = int.Parse(comboBoxMonthsInTheYear.SelectedItem.ToString());`. Also use `.SelectedValue` instead of `.SelectedItem` – boop_the_snoot Nov 01 '17 at 17:17

0 Answers0