2016-12-28 3 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

연산자> '날짜'와 '날짜'유형의 피연산자에 적용 할 수 없습니까? –

0

ComboBox에서 가지고있는 것에 달려 있습니다.

그냥 텍스트가있는 경우 : 당신이 ValueMember이 유형이다 오브젝트를 구속 한 경우

var dateFrom = Convert.ToDateTime(ComboBox1.Text); 
var dateTo = Convert.ToDateTime(ComboBox2.Text); 


if(dateFrom > dateTo) 
{ 
    // your code 
} 

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