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:
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.