重写/覆盖基类的事件

对于从父类继承来的事件,若子类需要在触发之后,进行 一些“特殊处理时”,建议:覆盖或重写父类的 <抽闲或虚的> OnXXX函数,若还需要保留父类事件触发后的某些特性,则需要  base.OnXXX()函数。PS:这些函数 <直接> 来自父类(注:这些函数在父类中是虚函数 或 抽象函数才可以),但源头可能是更底层的基类。

 public class CCIntAndUpperLetterBox : TextBox
    {
        private string lastText = "";

        protected override void OnTextChanged(TextChangedEventArgs e)
        {
            if (Text.Length > 0)
            {
                if (Text.Contains(" "))
                {
                    Text = Text.Replace(" ", "");
                    SelectionStart = Text.Length;
                }
                else
                {
                    if (StringHelper.IntAndUpper(Text))
                    {
                        Text = Text.ToUpper();
                        lastText = Text;
                    }
                    else
                    {
                        Text = lastText;
                    }
                }
            }
            else
            {
                lastText = "";
            }
            base.OnTextChanged(e);
        }
    }

原文地址:https://www.cnblogs.com/changbaishan/p/11208591.html