public dateinput()
{
// 该调用是 windows.forms 窗体设计器所必需的。
initializecomponent();
tbmonth.contextmenu = mnu ;
tbyear.contextmenu = mnu ;
tbday.contextmenu = mnu ;
this.height = 21 ;
this.value = datetime.now ;
this.dateformat = "yyyy-mm-dd" ;
if (!this.designmode)
createcalendar() ;
this.updown.width = 16 ;
this.refresh() ;
}
private void createcalendar()
{
calendar = new monthcalendar() ;
calendar.visible = true ;
calendar.dateselected+=new daterangeeventhandler(calendar_dateselected);
frmcalendar = new form() ;
frmcalendar.formborderstyle = formborderstyle.none ;
frmcalendar.topmost = true ;
frmcalendar.width = 270 ;
frmcalendar.height = 145 ;
frmcalendar.controls.add(calendar) ;
calendar.dock = dockstyle.fill ;
frmcalendar.startposition = formstartposition.manual ;
frmcalendar.deactivate+=new eventhandler(calendar_leave);
frmcalendar.showintaskbar = false ;
}
private void calendar_dateselected(object sender, system.windows.forms.daterangeeventargs e)
{
this.frmcalendar.hide() ;
this.value = this.calendar.selectionstart ;
this.tbday.focus() ;
}
private void calendar_leave(object sender, system.eventargs e)
{
this.frmcalendar.hide() ;
//this.value = this.calendar.selectionstart ;
this.tbday.focus() ;
}
private void refreshdisplay()
{
bool blgb = false ;
string strchar = "" ;
if (strdateformat == "yyyy-mm-dd")
strchar = "-" ;
else if (strdateformat == "yyyy.mm.dd")
strchar = "." ;
else if (strdateformat == "yyyy/mm/dd")
strchar = "/" ;
else if (strdateformat== "yyyy年mm月dd日")
blgb = true ;
else
strchar = strfomatchar ;
if (blgb)
{
lbsep1.text = "年" ;
lbsep2.text = "月" ;
lbsep3.text = "日" ;
}
else
{
lbsep1.text = strchar ;
lbsep2.text = strchar ;
lbsep3.text = "" ;
}
this.refresh() ;
}
/// <summary>
/// 是不是空值
/// </summary>
public bool isnull()
{
string stryear = tbyear.text ;
string strmonth = tbmonth.text ;
string strday = tbday.text ;
if (stryear == "" ||strmonth == "" ||strday == "" )
return true ;
else
return false ;
}
/// <summary>
/// 设置值为空(实际为时间的最小值)
/// </summary>
public void setvaluenull()
{
this.value = datetime.minvalue ;
}
/// <summary>
/// 清空输入值
/// </summary>
private void emptyinput()
{
tbyear.text = "" ;
tbmonth.text = "" ;
tbday.text = "" ;
}
private int getthismonthmaxday()
{
int year = int.parse(tbyear.text) ;
int month = int.parse(tbmonth.text) ;
switch(month)
{
case 2:
if (datetime.isleapyear(year))//闰年
return 29 ;
else
return 28 ;
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
return 31 ;
case 4:
case 6:
case 9:
case 11:
return 30 ;
default:
return 31 ;
}
}
private void setrightfmt()
{
if (isnull())
emptyinput() ;
else
{
setrightmonthday() ;
}
this.refresh() ;
}
private void setrightmonthday()
{
int maxday = getthismonthmaxday() ;
int day = int.parse(tbday.text) ;
if (day > maxday)
tbday.text = maxday.tostring() ;
}
private void incdecdate(int isign)
{
string stryear = tbyear.text ;
if (stryear == "")
stryear = datetime.now.year.tostring() ;
string strmonth = tbmonth.text ;
if (strmonth == "")
strmonth = datetime.now.month.tostring() ;
string strday = tbday.text ;
if (strday == "")
strday = datetime.now.day.tostring() ;
string strdate = stryear +"-"+ strmonth +"-" +strday ;
datetime dtold = datetime.parse(strdate) ;
if (tbyear.focused)
{
this.value = dtold.addyears(1*isign) ;
}
else if (tbmonth.focused)
{
this.value = dtold.addmonths(1*isign) ;
}
else
{
this.value = dtold.adddays(1*isign) ;
}
}
private form getwindow()
{
control con = this ;
while(!(con.parent is form))
con = con.parent ;
return (form)con.parent ;
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.dispose();
}
}
base.dispose( disposing );
}
