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

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

}






相關文章

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 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。