2016-12-28 147 views
0

我有两个组合框,一个是开始日期,另一个是结束日期。 我想要做if(combobox1 > combobox2)检查开始日期是否大于结束日期MessageBox.Show(“你选择了一个很好的开始日期”);比较两个组合框的值

这怎么办?

回答

0

就算这样简单:

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

CS1061 C#'object'不包含定义对于'toString'并且没有扩展方法'toString'接受类型'object'的第一个参数可以找到(你是否缺少使用指令或程序集引用?) –

+0

你能帮助我吗? –

+0

在类文件的顶部添加'using System.String' @ B.Pizhev – ViVi

0

这可能会解决问题

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

Operator>不能应用于'Date'和'Date'类型的操作数? –

0

这取决于你的组合框下的东西。

如果您只有文字:

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