想写一个自定义控件,让表单上所有的TextBox控件的Text值被改变时除了执行TextBox相应的事件外,再执行自定义控件中的自定义事件GeneralTextChanged应该怎么做?给个思路或者方法.谢谢!

解决方案 »

  1.   

    继承TextBox,再重写TextChanged事件
      

  2.   

    继承TextBox
    然后再写一些你需要的事件,然后再你需要的地方引发它们
      

  3.   

    晕了,你们没明白我的意思,我是要写另一个自定义控件,让表单上所有的TextBox控件的Text值被改变时除了执行TextBox相应的事件外,再执行自定义控件中的自定义事件GeneralTextChanged,我不是要扩展TextBox控件,我是写别一个新的控件.
      

  4.   

    你把你自定义控件的事件写好了
    在textbox的事件中触发不就行了
      

  5.   

    如果是WEB的话 ,我想可以用ASP.NET AJAX $ADDHANDler这个方法去做。
      

  6.   

    下面的代码,在Form中定义了一个GeneralTextChanged事件,然后把每个TextBox都挂上。
    放到你的控件里面是一样的。只需要TextBox.TextChanged 事件加上你的事件处理就行了。TextBox原来的TextChanged 事件处理还是会执行的。
     public FormInput()
            {
                InitializeComponent();
                
                
                this.GeneralTextChanged += new EventHandler(FormInput_GeneralTextChanged);            foreach (Control c in this.Controls)
                {
                    if (c is TextBox)
                    {
                        if (GeneralTextChanged != null)
                            c.TextChanged += GeneralTextChanged;                }
                }
            }
            private event EventHandler GeneralTextChanged;
            void FormInput_GeneralTextChanged(object sender, EventArgs e)
            {
                System.Console.WriteLine("FormInput_GeneralTextChanged");
            }
      

  7.   

    以组件的形式。。
    拖到窗体中,设置GeneralTextChanged事件。。
    然后在组件加载完后,比如InitializeComponent();之后或者在Load事件中加一个textChangedHook1.Hook(this);就行了
    参数就是容器,比如是Panel的话就把Panel内的所有文本框多挂一个TextChanged事件,是Form的话就是Form内的所有………………using System;
    using System.ComponentModel;
    using System.Windows.Forms;namespace Tsorgy.Utils
    {
        /// <summary>
        /// 挂钩父容器中所有 TextBox
        /// </summary>
        public class TextChangedHook : Component
        {
            [Category("Behavior")]
            [Description("文本框文字发生改变时触发的事件")]
            public event EventHandler GeneralTextChanged;        /// <summary>
            /// 处理 TextBox 控件的 TextChanged 事件。
            /// </summary>
            /// <param name="sender">事件源.</param>
            /// <param name="e">包含了事件数据的 <see cref="System.EventArgs"/> 实例</param>
            private void TextChangedHook_TextChanged(object sender, EventArgs e)
            {
                if (GeneralTextChanged != null)
                    GeneralTextChanged(sender, e);
            }        /// <summary>
            /// 挂钩 owner 的所有 TextBox 子控件
            /// </summary>
            /// <param name="owner"></param>
            public void Hook(Control owner)
            {
                if (owner == null)
                    return;
                foreach (Control c in owner.Controls)
                {
                    // 递归挂上所有子控件
                    Hook(c);
                    if (c is TextBox)
                        ((TextBox)c).TextChanged += new EventHandler(TextChangedHook_TextChanged);
                }
            }
        }
    }
      

  8.   

        void ddl_SelectedIndexChanged(object sender, EventArgs e)
        {
            DropDownList ddl = (DropDownList)sender;
            string sValue = ddl.SelectedValue;
            
            针对ddl选项变化的特殊事件在这里写
             针对ddl选项变化的通用事件在这里写
        }
        ddl.SelectedIndexChanged += new EventHandler(ddl_SelectedIndexChanged);每个控件的事件加上SelectedIndexChanged,不用再写什么自定义控件了, 很简单。
      

  9.   

    我觉得 你自己写一个容器控件。重写它的ADD或者对应的方法。。这样所有的TEXTBOX被ADD到你的控件里的时候,都注册他们的TEXTCHANGE事件。。在你的代码里处理你需要的动作 就可以
      

  10.   

    To:flyjimi 
    这一句有错误  c.TextChanged += GeneralTextChanged;
    错误 35 “System.Web.UI.Control”不包含“TextChanged”的定义,并且找不到可接受类型为“System.Web.UI.Control”的第一个参数的扩展方法“TextChanged”(是否缺少 using 指令或程序集引用?)
      

  11.   

    ((TextBox)c).TextChanged += .........