【问题标题】:Get Numeric Value of Month With Combo Box C#使用组合框 C# 获取月份的数值
【发布时间】:2019-02-14 22:07:44
【问题描述】:

我有一个combo box,我用它来选择一年中的一个月。月份是通过我设置为data sourceList<> 提供的。 我相信我的做法是错误的。

到目前为止的代码:

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

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

【问题讨论】:

  • 您应该调试您的代码并检查 cbMonth.SelectedText 的值是什么
  • 月份列表包含哪些对象?
  • 那么cbMonth.SelectedText 的值是多少 - 你检查过吗?我还想看看您是否可以将组合框项目的 value 与要显示的文本分开设置-我希望这是可能的,因此您可以适当地进行所有设置获取值。
  • @MikkaRin ,不过我已经调试过了,尽管选择了月份,但该值永远不会从空字符串改变。
  • @Lucian ,我的 List 包含一年中的月份(作为字符串)

标签: c# .net winforms datetime combobox


【解决方案1】:

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}");

【讨论】:

  • 你可能会问为什么Take(12),在MonthNames中不是默认12个月吗?阅读answer here.
【解决方案2】:

你试过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

【讨论】:

  • 奇怪.. 当我第一次尝试这种方法时,我一定是错误地使用了SelectedValue。你的回答对我有用!非常感谢!
  • 月份的数值选择索引+1。不需要解析日期时间。加载组合框,轻松获取价值this way.
【解决方案3】:

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

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

}

我希望这会有所帮助!

【讨论】:

  • 您可以在一行代码like this 中将月份加载到组合框中。
【解决方案4】:

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

【讨论】:

    【解决方案5】:

    从列表中获取数值,即

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

    【讨论】:

    • 我认为他说的是 WinForms 而不是 ASP.NET Webforms。
    • 啊,那样的话他就需要使用comboBox1.DisplayMember和comboBox1.ValueMember之间的区别
    【解决方案6】:

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

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

    【讨论】:

    • 无需解析。 SelectedIndex + 1 就够了。 Like this.
    猜你喜欢
    • 2010-12-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多