Error 1004
Error 1004

Reputation: 8240

Calculate month's days based on Year & Month selected

I am trying to get days number based on Year (Years are numbers) & Month (are text) selected from combo box.

Year Combo box name: cmbYear Month Combo box name: cmbMonth

Code trigger event:

    private void cmbMonth_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (cmbYear.SelectedIndex > -1)
        {
                {
                    var a = cmbDay;
                        a.Enabled = true;
                        a.BackColor = Color.LightCoral;
                }

                cmbMonth.BackColor = Color.Empty;
                MethodLibrary.Fill_cmbDay(cmbYear,cmbMonth, cmbDay);

Method:

static class MethodLibrary //Method does not return something
{
    public static void Fill_cmbDay(ComboBox strYear, ComboBox strMonth, ComboBox  cmbTarget) //Void used does not return something
    {
        //Find how many days month has based on selected year & month. Convert month name to month number.
        int days = DateTime.DaysInMonth(Convert.ToInt32(strYear.SelectedItem), Convert.ToInt32(strMonth.SelectedItem));

        //Clear Combo box
        cmbTarget.Items.Clear();

        //Loop from 1 to number of days & add items to combo box
        for (int i = 1; i <= days; i++)
        {
            cmbTarget.Items.Add(i);
        }
    }
}

UserForm:

enter image description here

Error on line:

int days = DateTime.DaysInMonth(Convert.ToInt32(strYear.SelectedItem), Convert.ToInt32(strMonth.SelectedItem));

I believe that error occurs during the conversion of strMonth.SelectedItem to int32.

An help will appreciate.

enter image description here

Upvotes: 0

Views: 245

Answers (2)

Error 1004
Error 1004

Reputation: 8240

What i have manage and it works for me:

Code trigger:

    private void cmbMonth_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (cmbYear.SelectedIndex > -1)
        {
                {
                    var a = cmbDay;
                        a.Enabled = true;
                        a.BackColor = Color.LightCoral;
                }

                cmbMonth.BackColor = Color.Empty;
                int monthInDigit = DateTime.ParseExact(cmbMonth.SelectedItem.ToString(), "MMMM", CultureInfo.InvariantCulture).Month;
                MethodLibrary.Fill_cmbDay(cmbYear, monthInDigit, cmbDay);
        }
    }

Method:

static class MethodLibrary //Method does not return something
{
    public static void Fill_cmbDay(ComboBox strYear, int Month, ComboBox  cmbTarget) //Void used does not return something
    {
        //Find how many days month has based on selected year & month. Convert month name to month number.
        int days = DateTime.DaysInMonth(Convert.ToInt32(strYear.SelectedItem),Month);

    //Clear Combo box
    cmbTarget.Items.Clear();

        //Loop from 1 to number of days & add items to combo box
        for (int i = 1; i <= days; i++)
        {
            cmbTarget.Items.Add(i);
        }
    }
}

Upvotes: 0

Dmitrii Bychenko
Dmitrii Bychenko

Reputation: 186843

The very reason of the exception is that your try to convert "January" string into integer value. Try

 int days = DateTime.DaysInMonth(
   Convert.ToInt32(strYear.SelectedItem), // "2019" can be converted into 2019
   strMonth.SelectedIndex + 1);           // "January" can't; let's take Index then  

Upvotes: 1

Related Questions