遗忘海岸

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

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

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   遗忘海岸  阅读(280)  评论(0编辑  收藏  举报

编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· [AI/GPT/综述] AI Agent的设计模式综述
历史上的今天:
2019-02-24 PB的一些记录
2016-02-24 arduino IO口
点击右上角即可分享
微信分享提示