Jaskier
Jaskier

Reputation: 1095

Get Numeric Value of Month With Combo Box C#

I have a combo box that I'm using to select a month out of a year. The months are provided through a List<> that I've set as the data source. I believe I'm going about this the wrong way.

Code So Far:

private void btnExport_Click(object sender, EventArgs e)
    {
        int month = 0; //just a default value
        if (cbMonth.SelectedText == "January")
            month = 1;
        else if (cbMonth.SelectedText == "Febuary")
            month = 2;
        else if (cbMonth.SelectedText == "March")
            month = 3;
        else if (cbMonth.SelectedText == "April")
            month = 4;
        else if (cbMonth.SelectedText == "May")
            month = 5;   
        else if (cbMonth.SelectedText == "June")
            month = 6;   
        else if (cbMonth.SelectedText == "July")
            month = 7;   
        else if (cbMonth.SelectedText == "August")
            month = 8;   
        else if (cbMonth.SelectedText == "September")
            month = 9;   
        else if (cbMonth.SelectedText == "October")
            month = 10;  
        else if (cbMonth.SelectedText == "November")
            month = 11;  
        else if (cbMonth.SelectedText == "December")
            month = 12;

        int year = Int32.Parse(mtbYear.Text);
        MessageBox.Show(month.ToString() + "   " + year.ToString()); // to check values

    }

My month never changes value and displays as 0. Which, I understand because I had given it the initial value of 0 in order to pass it to another method.

Question: How can I get the numeric value for the months when the user selects them from my combo box?

Upvotes: 0

Views: 1595

Answers (6)

Reza Aghaei
Reza Aghaei

Reputation: 125227

Show months in ComboBox:

comboBox1.DataSource = System.Globalization.CultureInfo.InvariantCulture
    .DateTimeFormat.MonthNames.Take(12).ToList();

Select current month:

comboBox1.SelectedIndex = DateTime.Now.Month - 1;

Get selected month:

MessageBox.Show($"Month: {comboBox1.SelectedIndex + 1} - {comboBox1.SelectedItem}");

Upvotes: 2

Lucian
Lucian

Reputation: 3554

Have you tried the SelectedValue of the Combobox?

private void btnExport_Click(object sender, EventArgs e)
    {
        int month = 0; //just a default value
        var monthNumber = DateTime.ParseExact((string)cbMonth.SelectedValue, "MMMM", CultureInfo.InvariantCulture).Month;

        int year = Int32.Parse(mtbYear.Text);
        MessageBox.Show(monthNumber.ToString() + "   " + year.ToString()); // to check values

    }

Don't forget to add try-catch for ParseExact

Upvotes: 1

Shanedolin
Shanedolin

Reputation: 59

Hey whenever you see yourself using this many if elses, try to simplify the logic.

IDictionary<string, int> monthsDictionary = new Dictionary<string, int>()
                                        {
                                            {January,"1"},
                                            {February, "2"},
                                            {March,"3"}
                                            /* And so on */
                                        };

Declare this dictionary with the months you are using. Then you can just look at what the selected value of the cbo box is and use that as the key. Just make sure that the values in the cbo box match the keys in the dictionary. Like so.

private void btnExport_Click(object sender, EventArgs e)
{
    month = monthsDictionary[cbMonth.SelectedText];
    //This will give you the value of the key.
    //Ex. If march is chosen then 3 is what is given back as an int.

    int year = Int32.Parse(mtbYear.Text);
    MessageBox.Show(month.ToString() + "   " + year.ToString()); // to check values

}

I hope this helps!

Upvotes: 1

Akshay Jain
Akshay Jain

Reputation: 790

Display the Month name at the font end but in value attribute store the month in number and when you select the month from combo box at that time in background you will get the month in digit

Upvotes: 0

cullimorer
cullimorer

Reputation: 755

To get the numeric value from a list, i.e.

<asp:DropDownList ID="cbMonth" runat="server">
    <asp:ListItem Selected="True" Value="1" Text="January"></asp:ListItem>
    <asp:ListItem Value="2" Text="February"></asp:ListItem>
</asp:DropDownList>

The Selected value of this will give you the numeric value:

   private void btnExport_Click(object sender, EventArgs e)
        {
            int month = cbMonth.SelectedValue;
        }

Upvotes: 0

Sham
Sham

Reputation: 930

You may use ParseExact method of DateTime. Ex -

var monthNumber = DateTime.ParseExact(cbMonth.SelectedText, "MMMM", CultureInfo.InvariantCulture).Month;

Upvotes: 0

Related Questions