ARTICLE

Custom Date Text Box

Posted by Ankur Gupta Articles | Active Directory in VB.NET May 15, 2010
In this article you will learn how to use Custom Date Text Box in VB.NET.
Download Files:
 
Reader Level:

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.

Login to add your contents and source code to this article
share this article :
post comment
 
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
Nevron Diagram
Become a Sponsor