/// <summary>
        /// Handles the KeyPress event.
        /// </summary>
        protected override void OnKeyPress(KeyPressEventArgs e)
        {
            if (!char.IsControl(e.KeyChar))
            {
                if (!ValidCharacters.Contains(e.KeyChar.ToString()))
                {
                    e.Handled = true;
                }

                if (Plus == e.KeyChar.ToString() && (SelectionStart != 0 || Text.StartsWith(Plus) || Text.StartsWith(Minus)))
                {
                    e.Handled = true;
                }

                if (Minus == e.KeyChar.ToString() && (SelectionStart != 0 || Text.StartsWith(Plus) || Text.StartsWith(Minus)))
                {
                    e.Handled = true;
                }

                double value   = 0;
                string newText = GetNewText(e.KeyChar);
                if (!string.IsNullOrEmpty(newText) && double.TryParse(newText, out value) &&
                    (value < Minimum || value > Maximum))
                {
                    e.Handled = true;
                }
            }

            // only allow one decimal separator
            if (e.KeyChar.ToString() == DecimalSeparator && Text.Contains(DecimalSeparator))
            {
                e.Handled = true;
            }
        }
Beispiel #2
0
        public static bool IsDnsNameValid(string dnsName)
        {
            if (dnsName.Length > MaximumLength)
            {
                return(false);
            }

            if (dnsName.Any(c => !ValidCharacters.Contains(c)))
            {
                return(false);
            }

            if (dnsName.First() == '-' || dnsName.Last() == '-')
            {
                return(false);
            }

            return(true);
        }
Beispiel #3
0
 private bool IsValid(char c)
 {
     return(ValidCharacters.Contains(c));
 }