| IsDateTime C# ErrorProvider |
| Section: WinForms | Rating: Not rated yet! |
 | CodeGod submitted this resource The member's homepage is http://www.codegod.de Visit the profile here |
public static class TypeHelper
{
/// <summary>
/// Check if an input-value is of type DateTime
/// </summary>
/// <param name="value">input</param>
/// <returns>bool</returns>
public static bool IsDateTime(object value)
{
try
{
if (value == null)
return false;
DateTime dateTime = DateTime.Parse(value.ToString());
return true;
}
catch (FormatException)
{
return false;
}
}
}
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnIsDateTime_Click(object sender, EventArgs e)
{
if (!TypeHelper.IsDateTime(textBox1.Text.Trim()))
errorProvider1.SetError(textBox1,
"You did not enter a valid DateTime-type");
else
errorProvider1.SetError(textBox1, "");
}
}
It's an easy example but you can extend it with other methods like IsNumeric... Have fun!