67 lines
2.0 KiB
C#
67 lines
2.0 KiB
C#
using System;
|
|
using System.Windows.Forms;
|
|
|
|
namespace TBF.UI.Item
|
|
{
|
|
public class EditFloat
|
|
{
|
|
string name;
|
|
float variable;
|
|
float lowerLimit;
|
|
float upperLimit;
|
|
string format;
|
|
TextBox textBox;
|
|
|
|
public EditFloat(string name, ref float variable, float lowerLimit, float upperLimit)
|
|
{
|
|
this.name = name;
|
|
this.variable = variable;
|
|
this.lowerLimit = lowerLimit;
|
|
this.upperLimit = upperLimit;
|
|
this.format = null;
|
|
|
|
this.textBox = new TextBox();
|
|
}
|
|
|
|
public EditFloat(string name, ref float variable, float lowerLimit)
|
|
: this(name, ref variable, lowerLimit, Single.MaxValue)
|
|
{
|
|
}
|
|
|
|
public EditFloat(string name, ref float variable)
|
|
: this(name, ref variable, Single.MinValue, Single.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.ParseSFloat(strValue); }
|
|
|
|
public bool Validate(string strValue, out string message)
|
|
{
|
|
float dummy;
|
|
if (!Utils.TryParseSFloat(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;
|
|
}
|
|
}
|
|
}
|
|
}
|