WinForm下常用的UI控制代码

0010: 控制Enter 转 Tab

        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)

        {

            if (keyData == Keys.Enter

                && (!(ActiveControl is TextBox) || !((TextBox)ActiveControl).AcceptsReturn)

                && (!(ActiveControl is Button || ActiveControl is UIButton)))

            {

                SendKeys.SendWait("{Tab}");

                return true;

            }

            if (keyData == (Keys.Enter | Keys.Shift))

            {

                SendKeys.SendWait("+{Tab}");

                return true;

            }

            return base.ProcessCmdKey(ref msg, keyData);

        }

0020: 控制TEXTBOX只能输入数字

            //全角占一个汉字,半角点半个汉字,所以在字节上是不同的
//全角数字"KeyChar"=2,半解数字"KeyChar"=1
byte[] array = System.Text.Encoding.Default.GetBytes(e.KeyChar.ToString());
//array.LongLength,而不是array.Length
if (!char.IsDigit(e.KeyChar) || array.LongLength == 2) e.Handled = true;
//'\b'是退格键值
if (e.KeyChar == '\b' || (e.KeyChar == '.'&&!sPhotoId.Text.Contains(".")))
e.Handled = false;

 

 

你可能感兴趣的:(WinForm)