Пример #1
0
        private void InputLimit(TextBox tBox, KeyPressEventArgs e, InputMod inputMod)
        {
            string strStart, strEnd;
            int    data;

            e.Handled = false;
            if (e.KeyChar == '\b')
            {
                return;                    //'b'为退格键
            }
            strStart = tBox.Text.Substring(0, tBox.SelectionStart);
            strEnd   = tBox.Text.Substring(tBox.SelectionStart + tBox.SelectionLength, tBox.Text.Length - tBox.SelectionStart - tBox.SelectionLength);


            switch (inputMod)
            {
            case InputMod.Int:
            {
                if (!Char.IsDigit(e.KeyChar) && e.KeyChar != '-')
                {
                    e.Handled = true;
                    return;
                }

                if (!int.TryParse(strStart + e.KeyChar + strEnd, out data) && (strStart + strEnd) != "")
                {
                    e.Handled = true;
                    return;
                }
                break;
            }

            case InputMod.UInt:
            {
                if (!Char.IsDigit(e.KeyChar))
                {
                    e.Handled = true;
                    return;
                }
                break;
            }

            default: e.Handled = true; break;
            }
        }
Пример #2
0
        /// <summary>
        /// 输入限制
        /// </summary>
        static public void InputLimit(TextBox textBox, KeyPressEventArgs e, InputMod inputMod)
        {
            string strStart, strEnd;

            e.Handled = false;
            if (e.KeyChar == '\b')
            {
                return;
            }

            strStart = textBox.Text.Substring(0, textBox.SelectionStart);
            strEnd   = textBox.Text.Substring(textBox.SelectionStart + textBox.SelectionLength,
                                              textBox.Text.Length - textBox.SelectionStart - textBox.SelectionLength);

            switch (inputMod)
            {
            case InputMod.Int:
            {
                int data;

                if (!Char.IsDigit(e.KeyChar) && e.KeyChar != '-')
                {
                    e.Handled = true;
                    return;
                }

                if (!int.TryParse(strStart + e.KeyChar + strEnd, out data) && (strStart + strEnd) != "")
                {
                    e.Handled = true;
                    return;
                }
                break;
            }

            case InputMod.UInt:
            {
                if (!Char.IsDigit(e.KeyChar))
                {
                    e.Handled = true;
                    return;
                }
                break;
            }

            case InputMod.Float:
            {
                float data;

                if (!Char.IsDigit(e.KeyChar) && e.KeyChar != '-' && e.KeyChar != '.')
                {
                    e.Handled = true;
                    return;
                }

                if (!float.TryParse(strStart + e.KeyChar + strEnd, out data) &&
                    (strStart + strEnd) != "" &&
                    (strStart + e.KeyChar + strEnd) != "-.")
                {
                    e.Handled = true;
                    return;
                }
                break;
            }

            case InputMod.UFloat:
            {
                float data;

                if (!Char.IsDigit(e.KeyChar) && e.KeyChar != '.')
                {
                    e.Handled = true;
                    return;
                }

                if (!float.TryParse(strStart + e.KeyChar + strEnd, out data) &&
                    (strStart + strEnd) != "")
                {
                    e.Handled = true;
                    return;
                }
                break;
            }

            case InputMod.Hex:
            {
                if (!Char.IsDigit(e.KeyChar) &&
                    !(e.KeyChar >= 'a' && e.KeyChar <= 'f') &&
                    !(e.KeyChar >= 'A' && e.KeyChar <= 'F'))
                {
                    e.Handled = true;
                    return;
                }
                break;
            }

            default: e.Handled = true; break;
            }
        }