可用來顯示空值的時間選擇控制項5

來源:互聯網
上載者:User
控制項|顯示 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




相關文章

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。