Comparing the values of 2 combo-boxes

1.2k Views Asked by At

I have two combo box, one with a start date, and the other is ending date. I want to do if(combobox1 > combobox2) check if the start date is greater than the ending date MessageBox.Show("You have chosen a great starting date of the final");

How can this be done?

5

There are 5 best solutions below

2
On

Just access both values of your ComboBoxes and then you can use DateTime.Compare method: https://msdn.microsoft.com/en-us/library/system.datetime.compare(v=vs.110).aspx

3
On

Simple as this :

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

This might fix the issue

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
On

It depends what you have under your ComboBoxes.

If you have just texts:

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


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

If you have bound objects where ValueMember is of Type DateTime

var dateFrom = (DateTime)ComboBox1.SelectedValue;
var dateTo = (DateTime)ComboBox2.SelectedValue;


if(dateFrom > dateTo)
{
   // your code
}
0
On
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");
}