以下のコードを使用して、必要な機能を実現しています。ユーザーが特定の NumericUpDown コントロールの値を編集しているときに、k
、K
、m
、またはM
のいずれかを押すと、現在入力されている量に を掛け1000
ます。また、オーバーフロー例外を回避したいと考えています。minimum
値は aと aで自動的に制限されますmaximum
。and関数が使用できるif
ため、ステートメントを使用しませんでした。しかし、そのロジックを処理するには、ある程度の精神的エネルギーが必要です (に適用するmin
max
min
maximum
max
minimum
... 何?)、「警告、このコードは読みにくいですが、機能します」という行に沿ってコメントを残す必要があるように感じました。これは私が書くべきコメントではありません。ロジックは単純すぎてコメントは必要ありませんが、それを表現する自明の方法を見つけることができません。助言がありますか?これを行うために、コントロール自体の設定/メソッドを使用できますか?
private void quantityNumericUpDown_KeyUp(object sender, KeyEventArgs e)
{
if (e.Control || e.Alt)
{
e.Handled = false;
return;
}
if (e.KeyCode != Keys.K && e.KeyCode != Keys.M)
{
e.Handled = false;
return;
}
e.SuppressKeyPress = true;
e.Handled = true;
this.Quantity *= OneThousand;
}
private decimal Quantity
{
get
{
return this.quantityNumericUpDown.Value;
}
set
{
// Sorry if this is not the most readable.
// I am trying to avoid an 'out of range' exception by clipping the value at min and max.
decimal valClippedUp = Math.Min(value, this.quantityNumericUpDown.Maximum);
this.quantityNumericUpDown.Value = Math.Max(valClippedUp, this.quantityNumericUpDown.Minimum);
}
}