Custom Date Text Box in VB.NETCustom Date Text Box
Add a User Control in VS application And Add a Text box and a dateTime pickler in it.
Add Date Time picker event
this.dateTimePicker1.ValueChanged += new System.EventHandler(this.dateTimePicker1_ValueChanged);
and write code in Events
private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
{
this.textBox1.Text = (DateTime.Parse(dateTimePicker1.Text)).ToString();
}
Add some browser properties
#region Browseable Property
[Browsable(true), Category("My Category")]
public string DBFieldName
{
get
{
return dbField;
}
set
{
dbField = value;
}
}
[Browsable(true), Category("My Category")]
public string GetStringDate
{
get
{
try
{
DateTime.Parse(this.textBox1.Text);
return this.textBox1.Text;
}
catch (Exception dtex)
{
return "Invalid Date";
}
}
}
[Browsable(true), Category("My Category")]
public void SetStringDate(string value)
{
textBox1.Text = value;
}
[Browsable(true), Category("My Category")]
public DateTime GetDate
{
get
{
try
{
return DateTime.Parse(this.textBox1.Text);
}
catch (Exception dtex)
{
return DateTime.Now;
}
}
}
[Browsable(true), Category("My Category")]
public string InvalidDateMessage
{
get
{
return invalidDateMessage;
}
set
{
invalidDateMessage = value;
}
}
[Browsable(true), Category("My Category")]
public bool IsRequired
{
get
{
return isRequired;
}
set
{
isRequired = value;
}
}
[Browsable(true), Category("My Category")]
public string RequiredMessage
{
get
{
return requiredMessage;
}
set
{
requiredMessage = value;
}
}
public void Enable(bool value)
{
if (value == false)
{
textBox1.Enabled = false;
dateTimePicker1.Enabled = false;
}
}
#endregion Browseable Property
Override onValidate events.
protected override void OnValidated(System.EventArgs e)
{
if (!validatedDate())
{
this.textBox1.BackColor = Color.Red;
this.textBox1.Focus();
}
else
{
this.textBox1.BackColor = Color.White;
this.textBox1.ForeColor = Color.Black;
}
}
You can download Full source code to find rest of the small things.