public class StringEventArgs : EventArgs
{
public String Value { get; set; }
}
public class ReadOnlyInputMgr
{
private Dictionary<int, String> _KeyValueDic =
new Dictionary<int, string>() {
{ 48, "0" }, { 49, "1" }, { 50, "2" }, { 51, "3" },
{52,"4"},{53,"5"},{54,"6"},{55,"7"},{56,"8"},
{57,"9"},{190,"."}
};
public EventHandler<StringEventArgs> OnDiameterInput;
private StringBuilder _Buffer = new StringBuilder();
public Boolean SuppressInput { get; set; }
public ReadOnlyInputMgr()
{
SuppressInput = false;
}
public ReadOnlyInputMgr(TextEdit txt)
: this()
{
txt.KeyDown += (s, e) =>
{
this.In(e.KeyValue);
};
OnDiameterInput += (s, e) =>
{
Console.WriteLine(e.Value);
SuppressInput = true;
txt.Text = e.Value;
SuppressInput = false;
};
}
public void In(int keyValue)
{
if (SuppressInput) return;
if (keyValue == 13)
{
double d = 0.0;
if (double.TryParse(_Buffer.ToString(), out d))
{
if (OnDiameterInput != null)
{
OnDiameterInput(this,new StringEventArgs(){Value= _Buffer.ToString().Trim()});
}
}
_Buffer.Clear();
}
else
{
if (_KeyValueDic.ContainsKey(keyValue))
{
_Buffer.Append(_KeyValueDic[keyValue]);
}
else
{
_Buffer.Clear();
}
}
}
}