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

问题描述 投票:0回答:6

我有一个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的初始值,以便将它传递给另一种方法。

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

c# .net winforms datetime combobox
6个回答
1
投票

你有没有尝试过Combobox的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


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

嘿,每当你看到自己使用这么多时,请尝试简化逻辑。

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

用你正在使用的月份声明这个字典。然后,您可以查看cbo框的选定值是什么,并将其用作关键字。只需确保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

}

我希望这有帮助!


0
投票

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

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

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>

Selected的选择值将为您提供数值:

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

0
投票

在字体末端显示月份名称,但在值属性中存储月份的数量,当您从背景中的组合框中选择月份时,您将获得月份的数字

© www.soinside.com 2019 - 2024. All rights reserved.