chenliezhong

导航

switch判断中多个case的简单优化 (二)

在上次做简单的计算器用字节数组写法,感觉看起来不直观明了。一下子很难看出来是前台UI界面传过来的下标值对应键盘的键值,所以用键值对修改了一下。

        Dictionary<int, byte> array = new Dictionary<int, byte>()
        {
            {0,0x30},{1,0x31},{2,0x32},{3,0x33},{4,0x34},{5,0x35},{6,0x36},{7,0x37},{8,0x38},{9,0x39},{10,0x08}
        };
       
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            if (e.Source is Button)
            {
                var btn = sender as Button;
                int num = int.Parse(btn.Content.ToString());
                Win32API.AddKeyBoardINput(array[num]);
            }
        }

替换下面的代码:

switch (btn.Content.ToString())
                {
                    case "0":
                        Win32API.AddKeyBoardINput(0x30);
                        break;
                    case "1":
                        Win32API.AddKeyBoardINput(0x31);
                        break;
                    case "2":
                        Win32API.AddKeyBoardINput(0x32);
                        break;
                    case "3":
                        Win32API.AddKeyBoardINput(0x33);
                        break;
                    case "4":
                        Win32API.AddKeyBoardINput(0x34);
                        break;
                    case "5":
                        Win32API.AddKeyBoardINput(0x35);
                        break;
                    case "6":
                        Win32API.AddKeyBoardINput(0x36);
                        break;
                    case "7":
                        Win32API.AddKeyBoardINput(0x37);
                        break;
                    case "8":
                        Win32API.AddKeyBoardINput(0x38);
                        break;
                    case "9":
                        Win32API.AddKeyBoardINput(0x39);
                        break;
                    case "X":
                        Win32API.AddKeyBoardINput(0X58);
                        break;
                }

posted on 2019-08-19 11:00  chenliezhong  阅读(729)  评论(0编辑  收藏  举报