2016-12-28 144 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"); 
}