tbf/TestBenchFramework/Boxes/IntBox.cs
2014-07-31 17:04:09 +02:00

143 lines
3.3 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; }
}
/// 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> Constructor that initializes the format </summary>
public IntBox(string format) { SetFormat(format); }
/// <summary> Constructor that initializes the complex format </summary>
public IntBox(string name, string format, string formatInvalid, string formatEx)
{
SetFormat(name, format, formatInvalid, formatEx);
}
/// <summary> Constructor that initializes the value and the format </summary>
public IntBox(int val, string format)
: this(val)
{
SetFormat(format);
}
/// <summary> Constructor that initializes the value and the complex format </summary>
public IntBox(int val, string name, string format, string formatInvalid, string formatEx)
: this(val)
{
SetFormat(name, format, formatInvalid, formatEx);
}
/// <summary>
/// Clear the box value and the valid flag
/// </summary>
public void Clear()
{
val = 0;
valid = false;
}
/// Format for a conversion to a string. In the most complex case the conversion is:
/// string.Format(FormatEx, val.ToString(Format))
string format = null;
string formatInvalid = "---";
string formatEx = "{0}";
/// <summary>
/// Set a format for the conversion to a string representation
/// </summary>
public void SetFormat(string format)
{
this.format = format;
}
public void SetFormat(string name, string format, string formatInvalid, string formatEx)
{
this.name = name;
this.format = format;
this.formatInvalid = formatInvalid;
this.formatEx = formatEx;
}
/// <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);
}
/// <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()
{
if (valid)
{
return new IntBox(val, name, format, formatInvalid, formatEx);
}
else
{
return new IntBox(name, format, formatInvalid, formatEx);
}
}
}
}