User Id :    Password :      New Member   Forgot Password  
 
Custom Date Text Box in VB.NET
Description In this article you will learn how to use Custom Date Text Box in VB.NET.   No. of Views     2233
  Rating     0
Author Ankur Gupta   Posted On     11 Jul 2010
Tags C#    

Sample Code   Download Code

Custom 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.

About Author

About Author I am Ankur Gupta Working in MNC as a sr. software engg. My major area of experiance in Accounting and CRM with C#,VB.Net,WCF,ASP.net languages. Ankur
No Photo
 
Country N/A
Company PILTD
Home Page N/A

Rate this article

Rating options from poor, fair, good, very good to excelent.  
 

Comments

   
Write your comment here.
Comment
Verification Code