0

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

Error 1004
  • 7,877
  • 3
  • 23
  • 46
  • 3
    `int days = DateTime.DaysInMonth(Convert.ToInt32(strYear.SelectedItem), strMonth.SelectedIndex + 1);` Since `"January"` is not a valid *integer* value – Dmitry Bychenko Apr 02 '19 at 09:14
  • Have you checked the input string when you debugged this error? What is the input string? – Mighty Badaboom Apr 02 '19 at 09:14
  • you can see [here](https://stackoverflow.com/questions/19220249/convert-a-string-containing-monthname-to-int-of-monthdigit) how to convert month name to month digit – styx Apr 02 '19 at 09:17

2 Answers2

1

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  
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
  • thanks for your respond. i make to convert month name to int using **int monthInDigit = DateTime.ParseExact(cmbMonth.SelectedItem.ToString(), "MMMM", CultureInfo.InvariantCulture).Month;** – Error 1004 Apr 02 '19 at 09:29
  • @Error 1004: Your are quite right, it's a correct conversion (a bit long, IMHO) – Dmitry Bychenko Apr 02 '19 at 09:33
  • i post my answer. If you have time to go through and if you can propose correction i ll glad to hear. – Error 1004 Apr 02 '19 at 09:38
0

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);
        }
    }
}
Error 1004
  • 7,877
  • 3
  • 23
  • 46
  • I'd rather *not* extract a `MethodLibrary` *class*: since it tied too tight with `Combobox`s which are on the form (since `Fill_cmbDay` is `public` I can call it as `Fill_cmbDay(cmbTarget, 47, strYear)`) – Dmitry Bychenko Apr 02 '19 at 09:46
  • When we want to add a *range* let's add a range, something like this `cmbTarget.Items.AddRange(Enumerable.Range(1, days))` – Dmitry Bychenko Apr 02 '19 at 09:46
  • If you provide `CultureInfo` *explicitly* `DateTime.ParseExact(cmbMonth.SelectedItem.ToString(), "MMMM", CultureInfo.InvariantCulture)` be sure that you use the same culture when filling in `cmbMonth` combobox – Dmitry Bychenko Apr 02 '19 at 09:48
  • Side note: (form design) - what is the desired behaviour if I click on maximize button on the "Registration Form". Is it *empty* and *gray* form with some controls in upper left corner? Often do we *remove* maximize and minimize buttons on dialog forms. – Dmitry Bychenko Apr 02 '19 at 09:52
  • For cmbMonth values, i import months names in the form load. for the maximization i have not done anything yet. – Error 1004 Apr 02 '19 at 09:54
  • Additionally, i try to add a range using the suggested method and i receive an error **Argument 1: cannot convert from 'System.Collections.Generic.IEnumerable' to 'object[]'** – Error 1004 Apr 02 '19 at 10:00
  • Add `ToArray()`: `Enumerable.Range(1, days).ToArray()` – Dmitry Bychenko Apr 02 '19 at 10:02
  • Argument 1: cannot convert from 'int[]' to 'object[]' another error occurred – Error 1004 Apr 02 '19 at 10:06
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/191080/discussion-between-error-1004-and-dmitry-bychenko). – Error 1004 Apr 02 '19 at 10:12