在TextChanged里面我想得到包含文本框的控件

时间:2015-01-07 18:11:06

标签: c# textbox

我有几个使用相同TextChanged方法的文本框。如何在这个TextChanged方法中获取包含触发事件的文本框的对象?

1 个答案:

答案 0 :(得分:0)

触发事件的TextBox包含在事件处理程序的sender参数中。你只需要写:

TextBox senderTB = (TextBox)sender;
Control container = senderTB.Parent;

//If container is custom
//You could cast or use as here depending on having a different parent is valid
MyControl customContainer = (MyControl)senderTB.Parent;

控件的容器位于Parent属性(MSDN)。