.NETte Dinge und noch einiges mehr ;-) RSS 2.0
 Friday, August 04, 2006
So jetzt kommt mal wieder seit längerer Zeit ein neue Codeschnippsel. Diesmal gehts um einen DateTimePicker der als eigenständiges Composite Control direkt eingebunden werden kann.

Das Datum kann ausgewählt werden über die Reiter. Derzeit werden bei den Tagen noch alle 31 möglichen Tage dargestellt. Beim Typecasting zu DateTime wird darauf geachtet und im Fall des 31. Februar der letztmögliche Tag ausgewählt. Also in diesem Fall der 29.
Der Buttontext und das Startjahr können über die Eigenschaften konfiguriert werden. Aber nun genug der Worte, hier ist der Code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace DateTimePicker
{
    /// <summary>
    /// raise if the button was clicked
    /// </summary>
    public delegate void ValueChanged(object sender, EventArgs e);
   
    /// <summary>
    /// DateTimerPicker creates three DropdownLists, so you can select a date
    /// </summary>
    [DefaultProperty("SelectedDate")]
    [ToolboxData("<{0}:DateTimePicker runat=server></{0}:DateTimePicker>")]
    public class DateTimePicker : Control, INamingContainer
    {
        private DateTime _DateTime;
        private DropDownList _Day;
        private DropDownList _Month;
        private DropDownList _Year;
        private short _StartYear = 1995;
        private short _YearCount = 20;
        private Button _Choose;
       
        public event ValueChanged ValueChanged;
       

        /// <summary>
        /// Gets or sets the selected date
        /// </summary>
        [Bindable(true)]
        [Category("Appearance")]
        [Localizable(true)]
        public DateTime SelectedDate
        {
            get
            {
                object dt = ViewState["DateTime"];
                return ((dt == null) ? DateTime.MinValue : (DateTime)dt);
            }

            set
            {
                ViewState["DateTime"] = value;
            }
        }

        /// <summary>
        /// Gets or sets the text for the choosing button
        /// </summary>
        [Bindable(true)]
        [Category("Appearance")]
        [Localizable(true)]
        public string ButtonText
        {
            get
            {
                return this._Choose.Text;
            }
            set
            {
                this._Choose.Text = value;
            }
        }

        /// <summary>
        /// Initialise a new instance
        /// </summary>
        public DateTimePicker()
        {
            this._Day = new DropDownList();
            this._Month = new DropDownList();
            this._Year = new DropDownList();
            this._DateTime = DateTime.Now;
            this._Choose = new Button();
            this._Choose.Click += new EventHandler(_Choose_Click);
           
            Initialize();
        }

       
        /// <summary>
        /// Initialise a new instance and sets the first year
        /// </summary>
        /// <param name="startYear">Set the first year in  DropDownList</param>
        /// <param name="yearCount">Set the Count of all years that display in DropDownList</param>
        public DateTimePicker(short startYear, short yearCount) : this()
        {
            this._StartYear = startYear;
            this._YearCount = yearCount;
        }

        void _Choose_Click(object sender, EventArgs e)
        {
            _Day_SelectedIndexChanged(sender, e);
        }

        protected override void CreateChildControls()
        {
            this.Controls.Add(this._Day);
            this.Controls.Add(this._Month);
            this.Controls.Add(this._Year);
            this.Controls.Add(this._Choose);
        }

        /// <summary>
        /// Readout the current values and set the SelectedDate
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void _Day_SelectedIndexChanged(object sender, EventArgs e)
        {
            int y = Convert.ToInt32(this._Year.SelectedValue);
            int m = Convert.ToInt32(this._Month.SelectedValue);
            int d = Convert.ToInt32(this._Day.SelectedValue);
           
            if (d > DateTime.DaysInMonth(y, m)) d = DateTime.DaysInMonth(y, m);
           
            this.SelectedDate = new DateTime(y, m, d);
           
            //Fire event if exists
            if (ValueChanged != null)
            {
                ValueChanged(this, null);
            }
        }

        /// <summary>
        /// Fill each list with values
        /// </summary>
        private void Initialize()
        {
            //the yearlist is the only one, that is parameteriseable
            for (int i = 0; i <= this._YearCount; i++)
            {
                ListItem item = new ListItem();
                int year = this._StartYear + i;
                item.Text = year.ToString();
                item.Value = year.ToString();
               
                this._Year.Items.Add(item);
            }

            //there are only and forever 12 month :-)
            for (int i = 1; i < 13; i++)
            {
                ListItem item = new ListItem();
                item.Text = i.ToString();
                item.Value = i.ToString();
               
                this._Month.Items.Add(item);
            }

            //TODO display not all 31 days for february
            for (int i = 1; i < 32; i++)
            {
                ListItem item = new ListItem();
                item.Text = i.ToString();
                item.Value = i.ToString();
               
                this._Day.Items.Add(item);
            }
        }
    }
}

DateTimePicker.zip (15.73 KB)
Friday, August 04, 2006 10:27:32 AM (W. Europe Standard Time, UTC+01:00)  #    Comments [0] -
ASP.NET
Comments are closed.
Archive
<January 2009>
SunMonTueWedThuFriSat
28293031123
45678910
11121314151617
18192021222324
25262728293031
1234567
About the author/Disclaimer

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

© Copyright 2009
Christian Stein
Sign In
Statistics
Total Posts: 238
This Year: 0
This Month: 0
This Week: 0
Comments: 20
All Content © 2009, Christian Stein
DasBlog theme 'Business' created by Christoph De Baene (delarou)