C# 自定义动态九宫格键盘,简单实用



    公司出了个需求,要求在自助机上动态生成绑定在文本框的数字键盘,除了0-9数组以外,左下角和右下角内容有可能不同,我为了不写死内容完成了这个Demo。具体使用方法如下。

类名:DiyKeyBoard.cs,参数类:DiyKeyBoardArgs,右下角完成有可能是删除有可能是完成,枚举 OperaType

 public class DiyKeyBoard : UserControl
    {
        Control passwordBox;
        WrapPanel MainWrapPanel;
        DiyKeyBoardArgs diyKeyBoardArgs = new DiyKeyBoardArgs();
        public delegate void ButtonSureLogin(object sender, DiyKeyBoardEventArgs diyKeyBoardEventArgs);
        /// <summary>
        /// 确定时间
        /// </summary>
        public ButtonSureLogin GotoLoginEvent;
        public DiyKeyBoard(Control _passwordBox, DiyKeyBoardArgs _diyKeyBoardArgs)
        {
            passwordBox = _passwordBox;
            diyKeyBoardArgs = _diyKeyBoardArgs;
            Content = InitPanel();

        }

   public WrapPanel InitPanel()
        {
   MainWrapPanel = new WrapPanel() { Width = diyKeyBoardArgs.DefaultWidth, Height = diyKeyBoardArgs.DefaultHeght };
            foreach (var key in diyKeyBoardArgs.KeyNums)
            {
                MainWrapPanel.Children.Add(MakeButton(key));
            }
            return MainWrapPanel;
        }

private Button MakeButton(string _Content)
        {
            Button button = new DiyKeyBoardButton() { Content = _Content, Width = (diyKeyBoardArgs.DefaultWidth) / diyKeyBoardArgs.col - 10, Height = (diyKeyBoardArgs.DefaultHeght) / diyKeyBoardArgs.row - 10, Margin = new Thickness(5), FontSize = diyKeyBoardArgs.DefaultFontSize };
            button.PreviewMouseLeftButtonDown -= Button_PreviewMouseLeftButtonDown;
            button.PreviewMouseLeftButtonDown += Button_PreviewMouseLeftButtonDown;
            return button;
        }

   private void Button_PreviewMouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            string OringlText = string.Empty;
            string buttonContent = (sender as Button).Content.ToString();
            //如果是密码型
            if ((passwordBox as PasswordBox) != null)
            {
                OringlText = (passwordBox as PasswordBox).Password;
            }
            //如果是文本型
            else if ((passwordBox as TextBox) != null)
            {
                OringlText = (passwordBox as TextBox).Text;
            }
            if (buttonContent.Equals(diyKeyBoardArgs.LeftDownButtonText))
            {
                if (GotoLoginEvent != null && GotoLoginEvent.GetInvocationList().Count() != 0)
                {
                    GotoLoginEvent.Invoke(this, diyKeyBoardArgs.diyKeyBoardEventArgs);
                }
            }
            else if (buttonContent.Equals(diyKeyBoardArgs.RightDownButtonText))
            {
                if (!string.IsNullOrEmpty(OringlText))
                {
                    OringlText = OringlText.Substring(0, OringlText.Length - 1);
                }
            }
            else
            {
                if (OringlText.Length < diyKeyBoardArgs.diyKeyBoardEventArgs.MaxLength)
                {
                    OringlText += buttonContent;
                }
            }
            //如果是密码型
            if ((passwordBox as PasswordBox) != null)
            {
                (passwordBox as PasswordBox).Password = OringlText;
            }
            //如果是文本型
            else if ((passwordBox as TextBox) != null)
            {
                (passwordBox as TextBox).Text = OringlText;
            }
        }
    }

class DiyKeyBoardArgs

  public class DiyKeyBoardArgs
    {
        public double DefaultWidth { get; set; } = 180;
        public double DefaultHeght { get; set; } = 180;
        public double DefaultFontSize { get; set; } = 180;
        public string LeftDownButtonText { get; set; } = "确定";
        public string RightDownButtonText { get; set; } = "删除";
        public List<string> KeyNums { get; set; } = new List<string>
        {
            "1","2","3","4","5","6","7","8","9","确定","0","删除"
        };
        public DiyKeyBoardEventArgs diyKeyBoardEventArgs { get; set; } = new DiyKeyBoardEventArgs() { operaType = OperaType.Complete };
        public int row { get; set; } = 3;
        public int col { get; set; } = 4;

        public int MaxLength { get; set; } = 18;
    }

   public enum OperaType
    {
        /// <summary>
        /// 拼接
        /// </summary>
        SubstringBuilding,
        /// <summary>
        /// 完成
        /// </summary>
        Complete
    }


使用方法

运行效果

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值