tbf/TBF/UI/Item/EditDouble.cs
2019-10-30 09:24:50 +01:00

67 lines
2.0 KiB
C#

using System;
using System.Windows.Forms;
namespace TBF.UI.Item
{
public class EditDouble : IEditItem
{
string name;
double variable;
double lowerLimit;
double upperLimit;
string format;
TextBox textBox;
public EditDouble(string name, ref double variable, double lowerLimit, double upperLimit)
{
this.name = name;
this.variable = variable;
this.lowerLimit = lowerLimit;
this.upperLimit = upperLimit;
this.format = null;
this.textBox = new TextBox();
}
public EditDouble(string name, ref double variable, double lowerLimit)
: this (name, ref variable, lowerLimit, Double.MaxValue)
{
}
public EditDouble(string name, ref double variable)
: this(name, ref variable, Double.MinValue, Double.MaxValue)
{
}
public string Name() { return name; }
public Control EmbeddedControl() { return textBox; }
public string Print() { return string.IsNullOrEmpty(format) ? variable.ToString() : variable.ToString(format); }
public void Update(string strValue) { variable = Utils.ParseSDouble(strValue); }
public bool Validate(string strValue, out string message)
{
double dummy;
if (!Utils.TryParseSDouble(strValue, out dummy))
{
message = string.Format("{0} is invalid", name);
return false;
}
else if (dummy < lowerLimit)
{
message = string.Format("{0} is smaller then {1}", name, lowerLimit);
return false;
}
else if (dummy > upperLimit)
{
message = string.Format("{0} is larger then {1}", name, upperLimit);
return false;
}
else
{
message = string.Empty;
return true;
}
}
}
}