遗忘海岸

江湖程序员 -Feiph(LM战士)

导航

ReadOnly TextEdit输入问题

    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();
                }
            }
        }
    }
View Code

 

posted on 2020-02-24 17:17  遗忘海岸  阅读(279)  评论(0编辑  收藏  举报