/// <summary> /// インスタンスを生成します。 /// </summary> public DateTextBox() { InitializeComponent(); // メンバ初期化 this.notationalDateType = Commons.Controls.NotationalDateType.YearAndMonthAndDay; this.monthCalendar.SelectionStart = default(DateTime); this.monthCalendar.SelectionEnd = default(DateTime); // 初期配置保持 this.initializeLabelFrontWidth = this.LabelFrontWidth; this.initializeLabelRearWidth = this.LabelRearWidth; this.initializeSize = this.Size; // イベント登録 base.CausesValidationChanged += this.DateTextBox_CausesValidationChanged; this.maskedTextBoxDate.MouseWheel += this.maskedTextBoxDate_MouseWheel; }
/// <summary> /// 指定された日付表記で正しい日付型かどうか判定します。 /// </summary> /// <param name="notationalDateType">日付表記</param> /// <returns>正しい日付の場合、true。正しい日付でない場合、falseを返します。</returns> private bool _ExactDateTime(NotationalDateType notationalDateType) { // 日付表記によって分岐し、日付文字列を取得 string dateString = String.Empty; switch (notationalDateType) { case NotationalDateType.YearAndMonthAndDay: dateString = this.maskedTextBoxDate.Text; break; case NotationalDateType.YearAndMonth: dateString = this.maskedTextBoxDate.Text + "01"; break; default: throw new InvalidEnumArgumentException(); } // 日付に変換できるか判定 DateTime dummy; string convertDateTimeString = this.ConvertDateTimeString(dateString); bool result = DateTime.TryParse(convertDateTimeString, out dummy); return result; }
/// <summary> /// 日付表記を変更します。 /// </summary> /// <param name="currentNotationalDateType">現在の日付表記種別</param> /// <param name="preNotationalDateType">前回の日付表記種別</param> private void ChangeNotationalDate(NotationalDateType currentNotationalDateType, NotationalDateType preNotationalDateType) { // 日付が入力されているか判定 bool isExactDateTim = this._ExactDateTime(preNotationalDateType); string dateString = "00010101".OverWrite(this.maskedTextBoxDate.Text); // 入力マスク変更 switch (currentNotationalDateType) { // 年月日 case NotationalDateType.YearAndMonthAndDay: this.maskedTextBoxDate.Mask = "0000年90月90日"; break; // 年月 case NotationalDateType.YearAndMonth: this.maskedTextBoxDate.Mask = "0000年90月"; break; default: throw new InvalidEnumArgumentException(); } // 日付が入力されていれば新たな日付表記に合わせて変更 if (true == isExactDateTim) { this.Value = DateTime.Parse(this.ConvertDateTimeString(dateString)); } return; }