【问题标题】:Comparing the values of 2 combo-boxes比较 2 个组合框的值
【发布时间】:2016-12-28 07:43:18
【问题描述】:

我有两个组合框,一个带有开始日期,另一个是结束日期。 我想做if(combobox1 > combobox2)检查开始日期是否大于结束日期 MessageBox.Show("You have selected a great start date of the final");

如何做到这一点?

【问题讨论】:

    标签: c# .net visual-studio combobox windows-forms-designer


    【解决方案1】:

    只需访问 ComboBoxes 的两个值,然后您就可以使用 DateTime.Compare 方法: https://msdn.microsoft.com/en-us/library/system.datetime.compare(v=vs.110).aspx

    【讨论】:

    • 日期是旧的,取自年/月/日结构的数据库
    • 然后你可以使用 DateTime.ParseExact 来设置你想要的格式
    【解决方案2】:

    这可能会解决问题

    var StartDate = comboBoxDate1.Text;
    var EndDate = comboBoxDate2.Text;
    var eDate = Convert.ToDateTime(EndDate);
    var sDate = Convert.ToDateTime(StartDate);
    if(StartDate != "" && StartDate != "" && sDate > eDate)
    {
        Console.WriteLine("Please ensure that the End Date is greater than the Start Date.");
    }
    

    【讨论】:

    • 运算符 > 不能应用于“日期”和“日期”类型的操作数?
    【解决方案3】:

    这取决于您的组合框下有什么。

    如果你只有文本:

    var dateFrom = Convert.ToDateTime(ComboBox1.Text);
    var dateTo = Convert.ToDateTime(ComboBox2.Text);
    
    
    if(dateFrom > dateTo)
    {
       // your code
    }
    

    如果您绑定了ValueMember 类型为DateTime 的对象

    var dateFrom = (DateTime)ComboBox1.SelectedValue;
    var dateTo = (DateTime)ComboBox2.SelectedValue;
    
    
    if(dateFrom > dateTo)
    {
       // your code
    }
    

    【讨论】:

      【解决方案4】:
      DateTime date1 = Convert.ToDateTime(comboBox1.Text);
      DateTime date2 = Convert.ToDateTime(comboBox2.Text);
      if(date1>date2)
      {
      MessageBox.Show("You have chosen a great starting date of the final");
      }
      

      【讨论】:

        【解决方案5】:

        就这么简单:

        DateTime d1 = Convert.ToDateTime(ComboBox1.SelectedValue.toString());
        DateTime d2 = Convert.ToDateTime(ComboBox2.SelectedValue.toString());
        if(d1 > d2)
        {
            MessageBox.Show("Some message");
        }
        

        【讨论】:

        • CS1061 C# 'object' 不包含 'toString' 的定义,并且找不到接受类型为 'object' 的第一个参数的扩展方法 'toString'(您是否缺少 using 指令或程序集参考?)
        • 你能帮帮我吗?
        • 在你的类文件@B.Pizhev上添加using System.String
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-05-19
        • 1970-01-01
        • 2019-09-26
        • 1970-01-01
        • 2023-03-26
        相关资源
        最近更新 更多