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

72 lines
1.8 KiB
C#

using System;
using System.Collections.Generic;
using System.Windows.Forms;
namespace TBF.UI.Item
{
public class EditEnum<T> : IEditItem where T : IComparable
{
string name;
T variable;
IEnumerable<T> values;
string[] strValues;
ComboBox comboBox;
public EditEnum(string name, ref T variable, IList<T>values, string[] strValues)
{
if (values.Count != strValues.Length) throw new Exception("values and strValues lengths are different");
this.name = name;
this.variable = variable;
this.values = values;
this.strValues = strValues;
this.comboBox = new ComboBox();
foreach (var v in strValues) comboBox.Items.Add(v);
}
public string Name() { return name; }
public Control EmbeddedControl() { return comboBox; }
public string Print()
{
int ix = 0;
foreach (var v in values)
{
if (v.CompareTo(variable) == 0) return strValues[ix];
ix++;
}
return string.Empty;
}
public void Update(string strValue)
{
int ix = 0;
foreach (var v in values)
{
if (strValues[ix++].Equals(strValue))
{
variable = v;
return;
}
}
}
public bool Validate(string strValue, out string message)
{
foreach (var sv in strValues)
{
if (sv == strValue)
{
message = string.Empty;
return true;
}
}
message = string.Format("{0} is invalid", name);
return false;
}
}
}