📄 datefield.cs
字号:
/** Copyright (c) 2006, All-In-One Creations, Ltd.* All rights reserved.* * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:* * * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.* * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.* * Neither the name of All-In-One Creations, Ltd. nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.* * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THEIMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE AREDISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLEFOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIALDAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS ORSERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVERCAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USEOF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.**//**
* Project: emergetk: stateful web framework for the masses
* File name: DateField.cs
* Description: A data selector, plain jane form inputs, or click the drop down to access the dojo datepicker.
*
* Author: Ben Joldersma
*
* @see The GNU Public License (GPL)
*/
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
using System;
using System.Collections;
using System.Globalization;
using System.Collections.Generic;
using EmergeTk.Model;
namespace EmergeTk.Widgets.Html
{
public delegate bool DateFieldChangedHandler( DateField sender, DateTime newValue );
/// <summary>
/// Summary description for DateField.
/// </summary>
public class DateField : Pane, IDataBindable
{
public override string ClientClass { get { return "Pane"; } }
private DateTime date = DateTime.Now;
/// <summary>
/// Property Date (DateTime)
/// </summary>
public DateTime Date
{
get
{
return this.date;
}
set
{
this.date = value;
SetChildwidgets();
RaisePropertyChangedNotification("Date");
}
}
/// <summary>
/// Property Label (string)
/// </summary>
new public string Label
{
get
{
return labelwidget.Text;
}
set
{
if (labelwidget.Text == null)
Widgets[0].InsertBefore(labelwidget);
labelwidget.Text = value;
}
}
private DropDown monthsDropDown;
private TextBox dateBox, yearBox;
static List<string> months;
static DateField()
{
months = new List<string>( DateTimeFormatInfo.CurrentInfo.MonthNames );
if (months[0] == string.Empty)
months.RemoveAt(0);
}
private Label labelwidget;
public DateField()
{
this.ClassName = "labeledTextBox";
labelwidget = new Label();
labelwidget.id = "label";
labelwidget.ClassName = "textBox";
monthsDropDown = new DropDown();
monthsDropDown.id = "month";
monthsDropDown.Options = months;
monthsDropDown.OnChanged += new WidgetChangedHandler(DropDown_OnChanged);
monthsDropDown.ClassName = "spaced";
dateBox = new TextBox();
dateBox.ClassName = "spaced";
dateBox.id = "date";
dateBox.Columns = 2;
dateBox.OnChanged += new WidgetChangedHandler(TextBox_OnChanged);
Literal l = new Literal();
l.id = "comma";
l.Html = ",";
yearBox = new TextBox();
yearBox.id = "year";
yearBox.Columns = 4;
yearBox.ClassName = "spaced";
yearBox.OnChanged += new WidgetChangedHandler(TextBox_OnChanged);
DatePicker dp = new DatePicker();
dp.id = "datepicker";
dp.OnSetDate += new EventHandler<DatePickerEventArgs>(dp_OnSetDate);
dp.Parent = this;
SetChildwidgets();
CalculateDate();
Add( monthsDropDown, dateBox, l, yearBox, dp );
ClientArguments["SetValue"] = "function(v) { SetDateFieldValue(this, v); }";
}
void dp_OnSetDate(object sender, DatePickerEventArgs e)
{
Value = e.NewDate;
yearBox.Text = e.NewDate.Year.ToString();
monthsDropDown.SelectedIndex = e.NewDate.Month - 1;
dateBox.Text = e.NewDate.Day.ToString();
propogate();
}
private void SetChildwidgets()
{
monthsDropDown.SelectedOption = DateTimeFormatInfo.CurrentInfo.GetMonthName( date.Month );
dateBox.Text = date.Day.ToString();
yearBox.Text = date.Year.ToString();
}
public event DateFieldChangedHandler OnChanged;
private void propogate()
{
if (OnChanged != null)
{
OnChanged(this, date);
}
}
private void Changed()
{
CalculateDate();
propogate();
}
private void CalculateDate()
{
Value = new DateTime( int.Parse( yearBox.Text ), monthsDropDown.SelectedIndex + 1, int.Parse( dateBox.Text ) );
}
public override string ToString()
{
return date.ToShortDateString();
}
private void TextBox_OnChanged(Widget sender, object newValue)
{
Changed();
}
private void DropDown_OnChanged(Widget sender, object newIndex)
{
Changed();
}
override public object Value
{
get
{
return this.Date;
}
set
{
this.Date = Convert.ToDateTime(value);
}
}
override public string DefaultProperty
{
get { return "Value"; }
}
private Binding binding;
public Binding Binding
{
get { return binding; }
set { binding = value; }
}
public bool IsPassThrough
{
get { return false; }
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -