我试图找到一种方法来轻松检测是否对winform上的控件进行了更改.此方法有效,但它不提供有关已更改控件的信息.有没有办法覆盖TextChanged事件,因此它将传递和EventArg,其中包含触发事件的控件的名称?当AccountChangedHandler执行发件人参数时,包含有关文本框的信息,例如'.Text'属性的当前值,但我没有看到有关哪个控件引发事件的任何信息.
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;
}
Run Code Online (Sandbox Code Playgroud)
void AccountChangedHandler(object sender, EventArgs e)
{
string n = ((TextBox)sender).Name;
string t = ((TextBox)sender).Text;
// or instead of cast
TextBox tb = sender as TextBox; // if sender is another type, tb is null
if(tb != null)
{
string n = tb.Name;
string t = tb.Text;
}
}
Run Code Online (Sandbox Code Playgroud)
你也可以尝试使用
foreach (Control c in this.Controls)
{
c.TextChanged += new EventHandler(AccountChangedHandler);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2103 次 |
| 最近记录: |