- 題名: DateTimePickerのインクリメント・ディクリメントについて
- 日時: 2005/02/08 21:36:41
- ID: 9006
- この記事の返信元:
- (なし)
- この記事への返信:
- [9011] Re[1]: DateTimePickerのインクリメント・ディクリメントについて2005/02/09 10:28:12
- ツリーを表示
皆さんのご意見をまとめると (C# ですけど) こんな感じでしょうか。
実験してみると OnKeyPress も override しておく必要があるようです。
たとえば、↓キーで 2005/2/1 まで下げた後、
テンキーで日の部分に 28 と入力すると
override すると 2005/2/28 となりますが、
しておかないと 2005/1/31 となってしまい、不自然な感じがします。
public class MyDateTimePicker : System.Windows.Forms.DateTimePicker
{
private DateTime oldValue;
public MyDateTimePicker() : base()
{
this.oldValue = this.Value;
}
private bool UpDownPressed = false;
protected override void OnKeyDown(KeyEventArgs e)
{
if ( ( e.KeyCode & Keys.Down ) == Keys.Down )
this.UpDownPressed = true;
else if ( ( e.KeyCode & Keys.Up ) == Keys.Up )
this.UpDownPressed = true;
else
this.UpDownPressed = false;
base.OnKeyDown (e);
}
protected override void OnKeyPress(KeyPressEventArgs e)
{
this.UpDownPressed = false;
base.OnKeyPress (e);
}
private bool ValueChanging = false;
protected override void OnValueChanged(EventArgs eventargs)
{
if ( this.ValueChanging )
return;
this.ValueChanging = true;
try
{
if ( this.UpDownPressed )
{
if ( this.oldValue.Day == 1 && this.Value.Day > 2 )
this.Value = this.oldValue.AddDays( -1 );
else if ( this.oldValue.Day > 2 && this.Value.Day == 1 )
this.Value = this.oldValue.AddDays( +1 );
else if ( this.oldValue.Month == 1 && this.Value.Month > 2 )
this.Value = this.oldValue.AddMonths( -1 );
else if ( this.oldValue.Month > 2 && this.Value.Month == 1 )
this.Value = this.oldValue.AddMonths( +1 );
}
}
finally
{
this.ValueChanging = false;
}
base.OnValueChanged (eventargs);
this.oldValue = this.Value;
}
}
分類:[.NET]
okaと申します。
いつも大変参考にしております。
本日はDateTimePickerについてご質問させてください。
DateTimePickerコントロールにフォーカスがあるときに、↑↓キー押下によりカーソル位置の値のインクリメントとディクリメントが発生しますが、このとき繰り上げと繰り下げ処理を行う事が出来ません。
例えば、「2005/01/01」という表示状態で日付の「01」にカーソルがあるとき、↓キーを押下すると「2004/12/31」と表示させたいのです。
現在の動きは「2005/01/31」となってしまいます。
つまり、カーソル位置のみのインクリメント・ディクリメントではなく、月末日や月初日、年末日等にカーソルがあたっていない箇所にも繰り上げ、繰り下げとして反映させたいのです。
上記問題の解決方法をご存知の方がいらっしゃれば、ご教授いただけないでしょうか?
よろしくお願いいたします。