Hi everybody,
Heres a few lines of code for a task that nearly drove me insane: convert, validate and compare dates from two different unbound MaskedTextBoxes. I hope it helps you guys out there... Special thanks to Cags and Marble Eater!
By for now,
JC
//------------------------------------------------------------------------------------------------------------------
class DateTimeUtils
{
public static bool IsDate(string strDate)
{
DateTime dtDate;
bool bValid = true;
try
{
dtDate = DateTime.Parse(strDate);
}
catch (FormatException)
{
MessageBox.Show("You have entered an invalid date. ", "Atention", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
bValid = false;
}
return bValid;
}
}
//------------------------------------------------------------------------------------------------------------------
private void DateIni_KeyUp(object sender, KeyEventArgs e)
{
if (this.DateIni.MaskCompleted)
{
string myDI = DateIni.Text;
if (DateTimeUtils.IsDate(myDI))
{
DateFin.Focus();
}
else
{
DateIni.Text = "";
DateIni.Focus();
}
}
}
//------------------------------------------------------------------------------------------------------------------
private void DateFin_KeyUp(object sender, KeyEventArgs e)
{
if (this.DateFin.MaskCompleted)
{
string myDI = DateIni.Text;
string myDF = DateFin.Text;
if (DateTimeUtils.IsDate(myDF))
{
DateTime DI = DateTime.Parse(myDI);
DateTime DF = DateTime.Parse(myDF);
if (DateTime.Compare(DI, DF) < 0)
{
EnableButtons();
}
else
{
MessageBox.Show("The final date value has to be bigger than the initial date value. ", "Atention", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
DisableButtons();
DateFin.Text = "";
DateFin.Focus();
}
}
}
}
//------------------------------------------------------------------------------------------------------------------
Heres a few lines of code for a task that nearly drove me insane: convert, validate and compare dates from two different unbound MaskedTextBoxes. I hope it helps you guys out there... Special thanks to Cags and Marble Eater!
By for now,
JC
//------------------------------------------------------------------------------------------------------------------
class DateTimeUtils
{
public static bool IsDate(string strDate)
{
DateTime dtDate;
bool bValid = true;
try
{
dtDate = DateTime.Parse(strDate);
}
catch (FormatException)
{
MessageBox.Show("You have entered an invalid date. ", "Atention", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
bValid = false;
}
return bValid;
}
}
//------------------------------------------------------------------------------------------------------------------
private void DateIni_KeyUp(object sender, KeyEventArgs e)
{
if (this.DateIni.MaskCompleted)
{
string myDI = DateIni.Text;
if (DateTimeUtils.IsDate(myDI))
{
DateFin.Focus();
}
else
{
DateIni.Text = "";
DateIni.Focus();
}
}
}
//------------------------------------------------------------------------------------------------------------------
private void DateFin_KeyUp(object sender, KeyEventArgs e)
{
if (this.DateFin.MaskCompleted)
{
string myDI = DateIni.Text;
string myDF = DateFin.Text;
if (DateTimeUtils.IsDate(myDF))
{
DateTime DI = DateTime.Parse(myDI);
DateTime DF = DateTime.Parse(myDF);
if (DateTime.Compare(DI, DF) < 0)
{
EnableButtons();
}
else
{
MessageBox.Show("The final date value has to be bigger than the initial date value. ", "Atention", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
DisableButtons();
DateFin.Text = "";
DateFin.Focus();
}
}
}
}
//------------------------------------------------------------------------------------------------------------------