C# 事件处理程序覆盖?
我正试图想出一种方法来轻松检测winform上的控件是否发生了更改。这种方法是有效的,但它不提供有关哪些控件已更改的信息。是否有方法重写TextChanged事件,使其传递包含触发事件的控件名称的EventArg?当AccountChangedHandler执行时,sender参数包含有关textbox的信息,例如“.Text”属性的当前值,但我看不到有关哪个控件引发事件的任何信息C# 事件处理程序覆盖?,c#,.net,winforms,C#,.net,Winforms,我正试图想出一种方法来轻松检测winform上的控件是否发生了更改。这种方法是有效的,但它不提供有关哪些控件已更改的信息。是否有方法重写TextChanged事件,使其传递包含触发事件的控件名称的EventArg?当AccountChangedHandler执行时,sender参数包含有关textbox的信息,例如“.Text”属性的当前值,但我看不到有关哪个控件引发事件的任何信息 private bool _dataChanged = false; internal TestUserCont
private bool _dataChanged = false;
internal TestUserControl()
{
InitializeComponent();
txtBillAddress1.TextChanged += new System.EventHandler(AccountChangedHandler);
txtBillAddress2.TextChanged += new System.EventHandler(AccountChangedHandler);
txtBillZip.TextChanged += new System.EventHandler(AccountChangedHandler);
txtBillState.TextChanged += new System.EventHandler(AccountChangedHandler);
txtBillCity.TextChanged += new System.EventHandler(AccountChangedHandler);
txtCountry.TextChanged += new System.EventHandler(AccountChangedHandler);
txtContactName.TextChanged += new System.EventHandler(AccountChangedHandler);
txtContactValue1.TextChanged += new System.EventHandler(AccountChangedHandler);
txtContactValue2.TextChanged += new System.EventHandler(AccountChangedHandler);
txtContactValue3.TextChanged += new System.EventHandler(AccountChangedHandler);
txtContactValue4.TextChanged += new System.EventHandler(AccountChangedHandler);
}
private void AccountChangedHandler(object sender, EventArgs e)
{
_dataChanged = true;
}
你也可以试着使用
foreach (Control c in this.Controls)
{
c.TextChanged += new EventHandler(AccountChangedHandler);
}
你也可以试着使用
foreach (Control c in this.Controls)
{
c.TextChanged += new EventHandler(AccountChangedHandler);
}
sender参数如何?sender参数如何?sender是对引发事件的控件的引用。如果你这样做
TextBox tb = sender as TextBox;
string name = tb.Name;
您将看到,现在您可以像使用txtContractName一样使用tb。如果你想做特定的逻辑,你可以做如下的事情
if(tb == txtBillAddress1) { ... }
但是,此时最好使用单独的事件处理程序。发送方是对引发事件的控件的引用。如果你这样做
TextBox tb = sender as TextBox;
string name = tb.Name;
您将看到,现在您可以像使用txtContractName一样使用tb。如果你想做特定的逻辑,你可以做如下的事情
if(tb == txtBillAddress1) { ... }
不过,在这一点上,最好使用单独的事件处理程序。Ahhh-因此引用在sender中!我在调试器监视窗口中查看发件人,但不太了解。你必须一直深入到“.base”,直到你在继承的基类中定义了.Name属性。如果我的文章有帮助,你能把它标记为答案吗?或者再问一个问题,我很乐意帮助你:啊哈-所以推荐信在发件人中!我在调试器监视窗口中查看发件人,但不太了解。你必须一直深入到“.base”,直到你在继承的基类中定义了.Name属性。如果我的文章有帮助,你能把它标记为答案吗?或者再问一个问题,我很乐意帮助你: