VIBlend
Home Products Downloads Purchase Support Forum About Blog

Events handling and validations in VIBlend DateTimePicker Control for Windows Forms

by viblend 22. October 2009 16:36

VIBlend Winforms DateTimePicker is a perfect solution when you need a  control which handles date and time input. In this blog post demonstrates how to use the control in one of the most common scenarios.

Below is a Windows Form with two datetimepicker controls. The controls allow the user to enter check-in and check-out dates.  

 

When the value of a datetimepicker is changed, the control raises the ValueChanged event. You can easily validate the user input by using the ValueChanged event and an ErrorProvider component.

For example, let’s handle the ValueChanged events of the two datetimepickers:

 

            this.dateTimePicker1.ValueChanged += new EventHandler(dateTimePicker1_ValueChanged);
            this.dateTimePicker2.ValueChanged += new EventHandler(dateTimePicker2_ValueChanged);
 

 

In the event handlers we validate the user input by comparing the Value properties:

 

        void dateTimePicker2_ValueChanged(object sender, EventArgs e)
        {
            if (this.dateTimePicker1.Value > this.dateTimePicker2.Value)
            {
                this.errorProvider1.SetError(this.dateTimePicker2, "Check in date should be before Check out date");
            }
            else
            {
                this.errorProvider1.Clear();
            }

            TimeSpan duration = this.dateTimePicker2.Value.Date - this.dateTimePicker1.Value.Date;
            this.length.Text = (int)duration.TotalDays + " night(s)";
        }



        void dateTimePicker1_ValueChanged(object sender, EventArgs e)
        {
            if (this.dateTimePicker1.Value > this.dateTimePicker2.Value)
            {
                this.errorProvider1.SetError(this.dateTimePicker1, "Check in date should be before Check out date");
            }
            else
            {
                this.errorProvider1.Clear();
            }

            TimeSpan duration = this.dateTimePicker2.Value.Date - this.dateTimePicker1.Value.Date;
            this.length.Text = (int)duration.TotalDays + " night(s)";
        }        

We compare the “check in” and “check out” dates and specify whether to show an error message by using the error provider. You can also specify the Minimum and Maximum properties which define the available range of correct date and time values:

You can see the full demo in our QSF in the MonthCalendar section.

About the author

Some text that describes me

Tag cloud

Recent comments

Comment RSS
Copyright © 2011 VIBlend  
ALL RIGHTS RESERVED  
 
Terms of Use | Privacy Policy
WinForms Controls Purchase Online About Us
       
DataGrid Navigation Pane Technical Support Blog
ScrollBar TreeView
ListBox ProgressBar Downloads Register
ComboBox Buttons
TabControl Editors Documentation Client Login

VIBlend Blog is powered by BlogEngine.NET