Identify which textbox has fired a text changed event

情到浓时终转凉″ 提交于 2019-12-01 21:06:31
Sjoerd

The sender parameter contains which control has fired the event. You can cast it to a TextBox and get the name property from it:

string propertyName = ((TextBox)sender).Name;

Cast object sender(your textbox which fired event) to TextBox.

If only one property is what you want then write

string propertyName = ((TextBox)sender).Name; 

But when more than one property is required, then it is better to create a Textbox variable and use it like.

TextBox txtbox =  (TextBox)sender;

Then you can use any property of it like

string propertyName = txtbox.Name; 

MessageBox.Show(proptertyName);
MessageBox.Show(txtbox.Content.ToString());

My advice is to look at the base class hierarchy at MSDN and just cast the control to it and extract the properties defined on it:

var name = ((ContentControl) sender).Name;

this is also a good practice for a more generic implementation because casting it to 'TextBox' means you can apply the handling logic to that type of control only.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!