Esempio n. 1
0
        /// <summary>
        /// repostion SelectionStart, recalculate SelectedLength
        /// </summary>
        protected override void OnKeyPress(KeyPressEventArgs e)
        {
            base.OnKeyPress(e);

            if (ReadOnly)
            {
                return;
            }

            if (e.KeyChar == (char)13 || e.KeyChar == (char)3 || e.KeyChar == (char)22 || e.KeyChar == (char)24)
            {
                return;
            }

            if (m_decimalLength == 0 && e.KeyChar == m_decimalSeparator)
            {
                e.Handled = true;
                return;
            }

            if (!m_allowNegative && e.KeyChar == m_negativeSign && base.Text.IndexOf(m_negativeSign) < 0)
            {
                e.Handled = true;
                return;
            }

            if (!char.IsDigit(e.KeyChar) && e.KeyChar != m_negativeSign && e.KeyChar != m_decimalSeparator)
            {
                e.Handled = true;
                return;
            }

            if (base.Text.Length >= m_MaxValueLength && e.KeyChar != m_negativeSign)
            {
                e.Handled = true;
                return;
            }

            if (e.KeyChar == m_decimalSeparator || e.KeyChar == m_negativeSign) // will position after dot(.) or first
            {
                SelectionLength = 0;
            }

            var isNegative = base.Text[0] == m_negativeSign ? true : false;

            if (isNegative && SelectionStart == 0)
            {
                SelectionStart = 1;
            }

            if (e.KeyChar == m_negativeSign)
            {
                var selStart = SelectionStart;

                if (!isNegative)
                {
                    base.Text      = m_negativeSign + base.Text;
                    SelectionStart = selStart + 1;
                }
                else
                {
                    base.Text = base.Text.Substring(1, base.Text.Length - 1);
                    if (selStart >= 1)
                    {
                        SelectionStart = selStart - 1;
                    }
                    else
                    {
                        SelectionStart = 0;
                    }
                }
                e.Handled = true; // minus(-) has been handled
                return;
            }

            var dotPos = base.Text.IndexOf(m_decimalSeparator) + 1;

            if (e.KeyChar == m_decimalSeparator)
            {
                if (dotPos > 0)
                {
                    SelectionStart = dotPos;
                }
                e.Handled = true; // dot has been handled
                return;
            }

            if (base.Text == "0")
            {
                BaseText.SelectAll(); //清除默认0
                //this.SelectionStart = 0;
                //this.SelectionStart = 1;  // replace thre first char, ie. 0
            }
            else if (base.Text == m_negativeSign + "0")
            {
                SelectionStart  = 1;
                SelectionLength = 1; // replace thre first char, ie. 0
            }
            else if (m_decimalLength > 0)
            {
                if (base.Text[0] == '0' && dotPos == 2 && SelectionStart <= 1)
                {
                    SelectionStart  = 0;
                    SelectionLength = 1; // replace thre first char, ie. 0
                }
                else if (base.Text.Substring(0, 2) == m_negativeSign + "0" && dotPos == 3 && SelectionStart <= 2)
                {
                    SelectionStart  = 1;
                    SelectionLength = 1; // replace thre first char, ie. 0
                }
                else if (SelectionStart == dotPos + m_decimalLength)
                {
                    SelectionStart -= 1;
                    SelectionLength = 1; // last position after text
                }
                else if (SelectionStart >= dotPos)
                {
                    SelectionLength = 1;
                }
                else if (SelectionLength == dotPos + m_decimalLength)
                {
                    ClearSelection();
                    SelectionStart  = 0;
                    SelectionLength = 1;
                }
            }
        }