使用组合框C#获取月份的数值

时间:2018-09-10 12:58:52

标签: c# .net winforms datetime combobox

我有一个combo box,用于选择一年中的一个月。我通过将List<>设置为data source来提供月份。 我相信我会以错误的方式解决这个问题。

到目前为止的代码:

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

    }

我的month从不更改值,并显示为0。据我所知,这是因为我已将其初始值0传递给了另一种方法。

问题:当用户从组合框中选择月份时,如何获得月份的数值?

6 个答案:

答案 0 :(得分:2)

ComboBox中显示月份:

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

选择当前月份:

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

获取所选月份:

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

答案 1 :(得分:1)

嘿,只要您发现自己使用了很多其他方法,就尝试简化逻辑。

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

用您使用的月份声明这本字典。然后,您可以查看cbo box的选定值是什么并将其用作键。只需确保cbo框中的值与字典中的键匹配即可。像这样。

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

}

我希望这会有所帮助!

答案 2 :(得分:1)

您是否尝试过组合框的SelectedValue?

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

    }

别忘了为ParseExact添加try-catch

答案 3 :(得分:0)

您可以使用DateTime的ParseExact方法。例如-

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

答案 4 :(得分:0)

要从列表中获取数值,即

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

此的选定值将为您提供数值:

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

答案 5 :(得分:0)

在字体末尾显示“月份”名称,但在“值”属性中以数字存储月份,当您在后台从组合框中选择月份时,您将以数字形式获得月份