131 lines
3.7 KiB
C#
131 lines
3.7 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Globalization;
|
|
using System.Linq;
|
|
using System.Media;
|
|
using System.Runtime.CompilerServices;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace W4Gui
|
|
{
|
|
public class FloatNumericUpDown : UpDownBase
|
|
{
|
|
private float value = 0;
|
|
private bool updating = false;
|
|
public float Value
|
|
{
|
|
get
|
|
{
|
|
if (this.UserEdit) ValidateEditText();
|
|
return this.value;
|
|
}
|
|
set
|
|
{
|
|
this.value = value;
|
|
UpdateEditText();
|
|
}
|
|
}
|
|
public override void DownButton()
|
|
{
|
|
try
|
|
{
|
|
this.Value--;
|
|
}
|
|
catch (OverflowException) { this.Value = Single.MinValue; };
|
|
}
|
|
|
|
public override void UpButton()
|
|
{
|
|
try
|
|
{
|
|
this.Value++;
|
|
}
|
|
catch (OverflowException) { this.Value = Single.MaxValue; };
|
|
}
|
|
|
|
protected override void OnLostFocus(EventArgs e)
|
|
{
|
|
base.OnLostFocus(e);
|
|
if (UserEdit)
|
|
{
|
|
UpdateEditText();
|
|
}
|
|
}
|
|
protected override void UpdateEditText()
|
|
{
|
|
if (this.updating) return;
|
|
|
|
this.updating = true;
|
|
if (Single.IsNaN(this.Value))
|
|
{
|
|
this.Text = "NaN";
|
|
}
|
|
if (Single.IsNegativeInfinity(this.Value))
|
|
{
|
|
this.Text = "-Inf";
|
|
}
|
|
if (Single.IsPositiveInfinity(this.Value))
|
|
{
|
|
this.Text = "+Inf";
|
|
}
|
|
else
|
|
{
|
|
string floatStr = Convert.ToDouble(this.Value).ToString("G9", CultureInfo.InvariantCulture);
|
|
if (!floatStr.Contains("."))
|
|
floatStr += ".00";
|
|
this.Text = floatStr;
|
|
|
|
}
|
|
this.updating = false;
|
|
}
|
|
|
|
protected override void OnTextBoxKeyPress(object? source, KeyPressEventArgs e)
|
|
{
|
|
base.OnTextBoxKeyPress(source, e);
|
|
|
|
if (Char.IsDigit(e.KeyChar)) return;
|
|
else if ("+-.infa\b".Contains(e.KeyChar.ToString().ToLower().First())) return;
|
|
else if ((Control.ModifierKeys & (Keys.Control | Keys.Alt)) != 0) return;
|
|
|
|
e.Handled = true;
|
|
SystemSounds.Beep.Play();
|
|
}
|
|
protected override void ValidateEditText()
|
|
{
|
|
try
|
|
{
|
|
this.value = Single.Parse(this.Text);
|
|
UpdateEditText();
|
|
}
|
|
catch (FormatException ex)
|
|
{
|
|
if (this.Text.Equals("NaN", StringComparison.InvariantCultureIgnoreCase))
|
|
this.value = Single.NaN;
|
|
else if (this.Text.Equals("Inf", StringComparison.InvariantCultureIgnoreCase))
|
|
this.value = Single.PositiveInfinity;
|
|
else if (this.Text.Equals("+Inf", StringComparison.InvariantCultureIgnoreCase))
|
|
this.value = Single.PositiveInfinity;
|
|
else if (this.Text.Equals("-Inf", StringComparison.InvariantCultureIgnoreCase))
|
|
this.value = Single.NegativeInfinity;
|
|
UpdateEditText();
|
|
}
|
|
catch (OverflowException ex)
|
|
{
|
|
if (this.Text.StartsWith("-")) this.Value = Single.MinValue;
|
|
else this.value = Single.MaxValue;
|
|
UpdateEditText();
|
|
}
|
|
catch
|
|
{
|
|
// Leave value alone
|
|
}
|
|
finally
|
|
{
|
|
UserEdit = false;
|
|
}
|
|
}
|
|
|
|
}
|
|
}
|