C# Winform限制文本框输入数字+字母+长度

作者:@大木瓜
本文为作者原创,转载请注明出处:https://www.cnblogs.com/damugua/p/16802756.html


复制代码
        private void txtFTrayCode_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar >= '0' && e.KeyChar <= '8'|| Char.IsLetter(e.KeyChar))//输入1-8位数字或字母
            {
                if (txtFTrayCode.Text.Length <= 7)
                {
                    e.Handled = false;    
                }
                else if (txtFTrayCode.SelectionLength > 1)
                {
                    e.Handled = false;//选择多个
                }
                else
                {
                    e.Handled = true;
                }
            }
            else if (e.KeyChar == '.' || e.KeyChar == (char)Keys.Return) //Tab或回车键
            {
                e.Handled = true;
                SendKeys.Send("{TAB}");
            }
            else if (e.KeyChar == '\b' || (int)e.KeyChar == 7)
                e.Handled = false;//删除
            else
                e.Handled = true;
        }
复制代码

 

posted @   大木瓜  阅读(527)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· AI与.NET技术实操系列(六):基于图像分类模型对图像进行分类
历史上的今天:
2021-10-18 SQL Server判断对象是否存在
点击右上角即可分享
微信分享提示