|
||||||
How to Add and Customize a C# Date Time PickerWorking with a DateTimePicker Object in a Windows ApplicationA Date Time Picker is a visual way of choosing a date in a Windows application. This interactive calendar can added to a form and customized with C#.
The date time picker is a very useful variation of the ComboBox used in many Windows form based applications. It appears on the form as a ComboBox containing a date however, if the application user clicks on it, it doesn't show a drop-down list. Instead it shows a calendar form which the user can select a date. Once the user has selected a date then that is shown in the ComboBox and its value can be used elsewhere in the application. The C# programmer will, therefore, be pleased to learn that the date time picker is very easy to work with. Adding a Date Time Picker to a C# Windows ApplicationA C# programmer adds a date time picker by creating a DateTimePicker object: using System;
using System.Windows.Forms;
using System.Drawing;
public class MainForm : Form {
private DateTimePicker datePicker = new DateTimePicker ();
This object can then be added to the form: public static void Main() {
Application.Run(new MainForm());
}
public MainForm() {
this.Controls.Add(datePicker);
If the form is compiled at this point then the user will see a simple form containing a DateTimePicker object, and if the user clicks on it then they will see the date time picker interactive calendar. However, the programmer can make some customizations to the way in which the object is displayed. For example they can:
Fortunately the C# code to do all of this is quite simple. Changing the Location of the Date Time Picker with C#The date time picker will appear in the top left corner of the form by default. However, the programmer can change this by using the object's "Location" property: datePicker.Location = new Point(50, 50);
Two values are passed to the point object used to set the location of the date time picker. These are the x and y coordinates and increasing them will move the object to the left and down the form. Changing the Format of the Date Shown in the Date Time PickerWhen the user opens the form then the date time picker will show then local system's long date format (for example "26 April 2006"). This can be changed to the short version (for example "26/4/2006"): datePicker.Format = DateTimePickerFormat.Short;
Or the programmer can supply their own custom date format: datePicker.CustomFormat = "dddd, d MMMM yyyy";
datePicker.Format = DateTimePickerFormat.Custom;
This time the date time picker will show something like "Sunday, 26 April 2009". Using the User's Date SelectionOnce the programmer has decided on a location and format for the date time picker then they can access the user's choice of date by accessing either of two properies:
The text propery contains the date as specified by the object's format and the value property contains a C# DateTime Object. SummaryThe C# DateTimePicker object provides a visual means of selecting a date in a Windows application. A programmer can:
And so they can supply their users with a means of selecting a date that is easy to use both by the user and the programmer.
The copyright of the article How to Add and Customize a C# Date Time Picker in Windows Programming is owned by Mark Alexander Bain. Permission to republish How to Add and Customize a C# Date Time Picker in print or online must be granted by the author in writing.
|
||||||
|
|
||||||
|
|
||||||