如何将一个特定控件作为事件的发件人? [英] How to make one particular control as Sender of a event?
本文介绍了如何将一个特定控件作为事件的发件人?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
private void EnterPressed( object sender, KeyEventArgs e )
{
if ( e.KeyCode == Keys.Enter )
{
if ( this.SignButton.Enabled == true &&
( sender is TextBox )
)
{
this.SignButton_Click( sender, e );
} // if
} // if
}
我的表单包含许多TextBox控件,如何区分发件人。
如果我传递一个特定的文本框名称,它会抛出错误为是一个字段但是用作类型
My Form consists of many TextBox Controls, how it will distinguish between the sender.
If i pass a particular textbox name, it is throwing error as "Is a field but used as type"
推荐答案
你需要做的就是将发送者对象类型转换为TextBox控件并检查是否为控件ID。这样的东西..
All you need to do here is to type cast the sender object to a TextBox Control and check to see is the control Id. Something like this..
TextBox ctrl = (TextBox) sender;
if (ctrl != null)
{
// Do something
// Check for condition based on control Id
if (ctrl.Id.equals("txt1"))
{
//do some thing
}
else if (ctrl.Id.equals("txt2")
{
// do something else..
}
}
这篇关于如何将一个特定控件作为事件的发件人?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文