C#高手请进,问一个思路或者方法 想写一个自定义控件,让表单上所有的TextBox控件的Text值被改变时除了执行TextBox相应的事件外,再执行自定义控件中的自定义事件GeneralTextChanged应该怎么做?给个思路或者方法.谢谢! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 继承TextBox,再重写TextChanged事件 继承TextBox然后再写一些你需要的事件,然后再你需要的地方引发它们 晕了,你们没明白我的意思,我是要写另一个自定义控件,让表单上所有的TextBox控件的Text值被改变时除了执行TextBox相应的事件外,再执行自定义控件中的自定义事件GeneralTextChanged,我不是要扩展TextBox控件,我是写别一个新的控件. 你把你自定义控件的事件写好了在textbox的事件中触发不就行了 如果是WEB的话 ,我想可以用ASP.NET AJAX $ADDHANDler这个方法去做。 下面的代码,在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"); } 以组件的形式。。拖到窗体中,设置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); } } }} void ddl_SelectedIndexChanged(object sender, EventArgs e) { DropDownList ddl = (DropDownList)sender; string sValue = ddl.SelectedValue; 针对ddl选项变化的特殊事件在这里写 针对ddl选项变化的通用事件在这里写 } ddl.SelectedIndexChanged += new EventHandler(ddl_SelectedIndexChanged);每个控件的事件加上SelectedIndexChanged,不用再写什么自定义控件了, 很简单。 我觉得 你自己写一个容器控件。重写它的ADD或者对应的方法。。这样所有的TEXTBOX被ADD到你的控件里的时候,都注册他们的TEXTCHANGE事件。。在你的代码里处理你需要的动作 就可以 To:flyjimi 这一句有错误 c.TextChanged += GeneralTextChanged;错误 35 “System.Web.UI.Control”不包含“TextChanged”的定义,并且找不到可接受类型为“System.Web.UI.Control”的第一个参数的扩展方法“TextChanged”(是否缺少 using 指令或程序集引用?) ((TextBox)c).TextChanged += ......... 请教关于listview选中状态的问题 C#如何取标题中的关键字? 动态加载程序集问题 好奇--关于msil的二次编译 winform里关于摄像头的问题 求教一个关于验证的问题~!谢谢大家帮忙。 如何在一个xml文件中进行查询操作? 简单问题,不记得怎么做了,人老了啊! 如何得到新插入记录的主键? 找不到文件“aspnet_regiis”(或他的组件之一)。怎么解决啊? C#新建Excel文件,并添加VBA宏代码、运行VBA宏代码 .Net 客户端访问 Java AXIS 开发的Webservice 遇到的问题
然后再写一些你需要的事件,然后再你需要的地方引发它们
在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");
}
拖到窗体中,设置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);
}
}
}
}
{
DropDownList ddl = (DropDownList)sender;
string sValue = ddl.SelectedValue;
针对ddl选项变化的特殊事件在这里写
针对ddl选项变化的通用事件在这里写
}
ddl.SelectedIndexChanged += new EventHandler(ddl_SelectedIndexChanged);每个控件的事件加上SelectedIndexChanged,不用再写什么自定义控件了, 很简单。
这一句有错误 c.TextChanged += GeneralTextChanged;
错误 35 “System.Web.UI.Control”不包含“TextChanged”的定义,并且找不到可接受类型为“System.Web.UI.Control”的第一个参数的扩展方法“TextChanged”(是否缺少 using 指令或程序集引用?)