示例#1
0
        public void AddValidation(Control tb, StringRule rule)
        {
            if (ruleMap.ContainsKey(tb))
            {
                return;
            }

            tb.TextChanged += Tb_TextChanged;
            ruleMap.Add(tb, rule);

            if (rule.Pass(tb.Text))
            {
                textBeforeChange.Add(tb, tb.Text);
            }
            else
            {
                throw new TextBoxValueDoesntMatchRuleException($"<{tb.Name}>的Text属性值不满足正则表达式:{rule.ToString()}");
            }
        }
示例#2
0
        private void Tb_TextChanged(object sender, System.EventArgs e)
        {
            TextBox    tb   = sender as TextBox;
            StringRule rule = ruleMap[tb];

            if (rule.Pass(tb.Text))
            {
                textBeforeChange[tb] = tb.Text;
                errorProvider.SetError(tb, "");
                return;
            }

            else
            {
                errorProvider.SetError(tb, rule.Hint);
                if (ForbiddenOutRangeInput)
                {
                    tb.Text = textBeforeChange[tb];
                }
                UnvalidatedInput?.Invoke(tb, rule);
            }
        }