private void tbDay_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
if (this.DesignMode)
return ;
TextBox obj = (TextBox)sender ;
obj.SelectedText = "" ;
string strValue = obj.Text.Trim() ;
int iLen= strValue.ToString().Length ;
int key = (int)(e.KeyChar) ;
switch(key)
{
case 48://0
case 49://1
case 50://2
case 51://3
case 52://4
case 53://5
case 54://6
case 55://7
case 56://8
case 57://9
if (iLen == 0)
e.Handled = false ;
else if (iLen == 1)//已经输入了一位
{
e.Handled = true ; //默认不处理,后面分析需要处理的情况
if (key==48||key==49||key==50)//现在输入的是0,1,2
{
if(obj.SelectionStart == 0)//插入
{
if (strValue != "0")//已经输入的不是0
e.Handled = false ;
if (strValue == "0" &&(key==49||key==50))
e.Handled = false ;
}
else
{
if (strValue == "0"||strValue == "1"||strValue == "2"||strValue == "3")//追加且已经输入了0,1,2,3
e.Handled = false ;
if (key==48&&strValue == "0")
e.Handled = true ;
if (key==50&&strValue == "3")
e.Handled = true ;
}
}
else//输入的是非0,1,2数字
{
if (strValue == "0"||strValue == "1"||strValue == "2")//已经输入了一个0,1,2
{
if (obj.SelectionStart == 0)//前面插入数字
{
if (key==51&&strValue != "2")//只能插入3
e.Handled = false ;
}
else
e.Handled = false ;
}
}
}
else
e.Handled = true ;
break ;
case 45://-
e.Handled = true ;
break ;
case 8://backspace
if (iLen == 0)
obj.Parent.SelectNextControl(obj,false,true,false,true) ;
break ;
case 27://esc
e.Handled = false ;
break ;
default :
e.Handled = true ;
break ;
}
}
private void tbMonth_Enter(object sender, System.EventArgs e)
{
if (this.DesignMode)
return ;
TextBox obj = (TextBox)sender ;
obj.SelectAll() ;
}
private void tbMonth_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
if (this.DesignMode)
return ;
TextBox obj = (TextBox)sender ;
int iValue = e.KeyValue ;
switch(iValue)
{
case 37://left
if (obj.SelectionStart == 0)
{
obj.Parent.SelectNextControl(obj,false,true,false,true) ;
}
break ;
case 38://top
IncDecDate(1) ;
break ;
case 39://right
if (obj.SelectionStart == obj.Text.Length)
{
obj.Parent.SelectNextControl(obj,true,true,false,true) ;
}
break ;
case 40://down
IncDecDate(-1) ;
break ;
}
}
private void DateInput_Leave(object sender, System.EventArgs e)
{
SetRightFmt() ;
}
private void tbYear_Leave(object sender, System.EventArgs e)
{
String strYear = tbYear.Text ;
int iLen = strYear.Length ;
switch(iLen)
{
case 0:
break ;
case 1:
tbYear.Text = "200"+strYear ;
break ;
case 2:
if (int.Parse(strYear)<40)
tbYear.Text = "20"+strYear ;
else
tbYear.Text = "19"+strYear ;
break ;
}
}
private void tbMonth_Leave(object sender, System.EventArgs e)
{
if (tbMonth.Text!="" &&tbDay.Text !="")
SetRightMonthDay() ;
}
private void UpDown_Scroll(object sender, System.Windows.Forms.ScrollEventArgs e)
{
if (this.DesignMode)
return ;
switch(e.Type)
{
case ScrollEventType.SmallIncrement:
IncDecDate(-1) ;
break ;
case ScrollEventType.SmallDecrement:
IncDecDate(1) ;
break ;
}
}
private void btnMain_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
Control con = (Control)sender ;
int BaseLen = con.Height / 6 ;
Point pos1 = new Point(con.Width/2 - BaseLen-2,BaseLen*2) ;
Point pos2 = new Point(con.Width/2 + BaseLen+2,BaseLen*2) ;
Point pos3 = new Point(con.Width/2,BaseLen*4) ;
Point[] mulPos = {pos1,pos2,pos3} ;
if (btnMain.Enabled)
{
e.Graphics.DrawPolygon(Pens.Black,mulPos) ;
e.Graphics.FillPolygon(Brushes.Black,mulPos) ;
}
else
{
e.Graphics.DrawPolygon(Pens.DarkGray,mulPos) ;
e.Graphics.FillPolygon(Brushes.DarkGray,mulPos) ;
}
}
private void btnMain_Click(object sender, System.EventArgs e)
{
if (this.DesignMode)
return ;
if (frmCalendar.Visible)
{
frmCalendar.Visible = false ;
return ;
}
else
{
Point pos = this.Location ;
pos.Y = pos.Y + this.Height ;
pos = this.Parent.PointToScreen(pos) ;
pos = SetCalendarPos(pos,frmCalendar) ;
frmCalendar.SetDesktopLocation(pos.X,pos.Y) ;
frmCalendar.Visible = true ;
}
}
private Point SetCalendarPos(Point sPos,Control ConToShow)
{
Point NewPos =new Point(sPos.X,sPos.Y);
int iConWidth = ConToShow.Width ;
int iConHeight = ConToShow.Height ;
int iScrWidth = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Width ;
int iScrHeight = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Height ;
if (sPos.Y + iConHeight>iScrHeight)
NewPos.Y = iScrHeight - iConHeight ;
if (sPos.X + iConWidth>iScrWidth)
NewPos.X = iScrWidth - iConWidth ;
return NewPos ;
}
private void lbSpace_Click(object sender, System.EventArgs e)
{
if (this.DesignMode)
return ;
tbDay.Focus() ;
tbDay.SelectAll() ;
}
private void lbSep1_EnabledChanged(object sender, System.EventArgs e)
{
Label label = (Label)sender;
if (label.Enabled)
label.BackColor = Color.FromKnownColor(KnownColor.Window) ;
else
label.BackColor = Color.FromKnownColor(KnownColor.Control) ;
}
}//end class
public class DateFormatConverter:System.ComponentModel.StringConverter
{
/// <summary>
/// 根据返回值确定是否支持下拉框的形式
/// </summary>
/// <returns>
/// true: 下拉框的形式
/// false: 普通文本编辑的形式
/// </returns>
public override bool GetStandardValuesSupported(System.ComponentModel.ITypeDescriptorContext context)
{
return true;
}
/// <summary>
/// 下拉框中具体的内容
/// </summary>
public override System.ComponentModel.TypeConverter.StandardValuesCollection GetStandardValues(System.ComponentModel.ITypeDescriptorContext context)
{
return new StandardValuesCollection(new string[]{"yyyy-MM-dd","yyyy.MM.dd","yyyy/MM/dd","yyyy年MM月dd日","自定义"});
}
public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType)
{
if( sourceType == typeof(string) )
return true;
else
return base.CanConvertFrom(context, sourceType);
}
/// <summary>
/// 根据返回值确定是否是不可编辑的文本框
/// </summary>
/// <returns>
/// true: 文本框不可以编辑
/// flase: 文本框可以编辑
/// </returns>
public override bool GetStandardValuesExclusive(System.ComponentModel.ITypeDescriptorContext context)
{
return true;
}
}
}//end namespace