天天看点

TextBox 数字,范围限制

直接上代码

/// <summary>
        /// 文本框文本输入事件
        /// </summary>
        private void txt_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            Regex regex = new Regex("^[0-9]+$");
            e.Handled = !regex.IsMatch(e.Text);

            if (e.Handled)
                return;

            try
            {
                TextBox txtBox = sender as TextBox;

                string strTag = txtBox.Tag.ToString().Replace("Seconds", "").Replace("ms", "").Replace("s", "").Replace("d", "").Replace("%", "")
                    .Replace("(", "").Replace(")", "").Replace("(", "").Replace(")", "");
                string strText = txtBox.Text + e.Text;

                int max = 0;
                int min = 0;

                if (!string.IsNullOrWhiteSpace(strTag))
                {
                    string[] strs = strTag.Split('-');
                    max = min = int.Parse(strs[0]);
                    foreach (string str in strs)
                    {
                        int num = int.Parse(str);
                        if (num < min)
                            min = num;
                        if (num > max)
                            max = num;
                    }
                }

                int number = int.Parse(strText);
                if (number >= min && number <= max)
                    e.Handled = false;
                else
                {
                    e.Handled = true;
                    if (number < min)
                        txtBox.Text = min.ToString();
                    if (number > max)
                        txtBox.Text = max.ToString();
                }
            }
            catch (Exception ex)
            {
                e.Handled = true;
            }
        }

        /// <summary>
        /// 键盘按键事件
        /// 禁用粘贴
        /// </summary>
        private void txt_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            TextBox txtBox = sender as TextBox;
            if (txtBox.SelectionLength > 0)
                txtBox.Select(txtBox.Text.Length, 0);

            if ((e.KeyStates == Keyboard.GetKeyStates(Key.LeftCtrl) || e.KeyStates == Keyboard.GetKeyStates(Key.RightCtrl)) && e.KeyStates == Keyboard.GetKeyStates(Key.V))
                e.Handled = true;
            else
                e.Handled = false;

            if (e.KeyStates == Keyboard.GetKeyStates(Key.Back))
            {
                try
                {
                    string strTag = txtBox.Tag.ToString().Replace("Seconds", "").Replace("ms", "").Replace("s", "").Replace("d", "").Replace("%", "")
                        .Replace("(", "").Replace(")", "").Replace("(", "").Replace(")", "");
                    string strText = txtBox.Text.Remove(txtBox.Text.Length - 1, 1);

                    int max = 0;
                    int min = 0;

                    if (!string.IsNullOrWhiteSpace(strTag))
                    {
                        string[] strs = strTag.Split('-');
                        max = min = int.Parse(strs[0]);
                        foreach (string str in strs)
                        {
                            int num = int.Parse(str);
                            if (num < min)
                                min = num;
                            if (num > max)
                                max = num;
                        }
                    }

                    if (strText.Length == 0)
                    {
                        txtBox.Text = min.ToString();
                        e.Handled = true;
                        return;
                    }

                    int number = int.Parse(strText);
                    if (number >= min && number <= max)
                        e.Handled = false;
                    else
                    {
                        e.Handled = true;
                        if (number < min)
                            txtBox.Text = min.ToString();
                        if (number > max)
                            txtBox.Text = max.ToString();
                    }
                }
                catch (Exception ex)
                {
                    e.Handled = false;
                }
            }
        }