tbf/TestBenchFramework/Boxes/IntBox.cs
Milan Hanajik 836daaa5a1 - German translations
- IBox, FloatBox, IntBox related changes
2014-07-31 22:17:48 +02:00

119 lines
2.7 KiB
C#

using System;
using System.Text;
namespace TBF.Boxes
{
public class IntBox : IBox
{
/// Box value
private int val;
public int Val
{
get { return (valid ? val : 0); }
set { val = value; valid = true; }
}
/// Box name
private string name;
public string Name
{
get { return name; }
set { name = value; }
}
/// Validity flag: true = the box value is valid
private bool valid = false;
public bool Valid
{
get { return valid; }
}
/// <summary> Parameterless constructor </summary>
public IntBox()
{
}
/// <summary> Constructor that initializes the value </summary>
public IntBox(int val)
{
this.Val = val;
}
/// <summary>
/// Clear the box value and the valid flag
/// </summary>
public void Clear()
{
val = 0;
valid = false;
}
///
/// Specifies a conversion to a string.
/// In the most complex case the conversion is:
/// string.Format(FormatEx, val.ToString(Format))
/// If the value is invalid, the conversion result is 'FormatInvalid'.
///
public string Format = null;
public string FormatEx = "{0}";
public string FormatInvalid = "---";
public float LimitLo = int.MinValue;
public float LimitHi = int.MaxValue;
/// <summary>
/// Converts the box value to a string representation
/// </summary>
public override string ToString()
{
if (!valid)
{
return FormatInvalid;
}
else if (Format == null)
{
return string.Format(FormatEx, Val);
}
else
{
return string.Format(FormatEx, Val.ToString(Format));
}
}
/// <summary>
/// Validates the string representation of the box value
/// </summary>
/// <param name="strVal">String representation of the parameter</param>
/// <returns>true = string is OK, false = string is wrong (see 'message')</returns>
public bool ValidateParam(string strVal)
{
int dummy;
return int.TryParse(strVal, out dummy) && (dummy >= LimitLo) && (dummy <= LimitHi);
}
/// <summary>
/// Update the parameter from a string
/// </summary>
/// <param name="strVal">String representation of the box value</param>
public void UpdateParam(string strVal)
{
try
{
Val = int.Parse(strVal);
}
catch { };
}
public IntBox Clone()
{
IntBox newFloatBox = new IntBox();
newFloatBox.name = name;
newFloatBox.val = val;
newFloatBox.valid = valid;
newFloatBox.Format = Format;
newFloatBox.FormatInvalid = FormatInvalid;
newFloatBox.FormatEx = FormatEx;
return newFloatBox;
}
}
}