让我以示例开头:
我<div class="head">Head</div>
<div class="content">
<p>asdfsadf asikfdj sadlkf jasilkfr ioawfh askjh dfkjsdahf kasjdfh sa</p>
<p>asdfsadf asikfdj sadlkf jasilkfr ioawfh askjh dfkjsdahf kasjdfh sa</p>
<p>asdfsadf asikfdj sadlkf jasilkfr ioawfh askjh dfkjsdahf kasjdfh sa</p>
<p>asdfsadf asikfdj sadlkf jasilkfr ioawfh askjh dfkjsdahf kasjdfh sa</p>
<p>asdfsadf asikfdj sadlkf jasilkfr ioawfh askjh dfkjsdahf kasjdfh sa</p>
<p>asdfsadf asikfdj sadlkf jasilkfr ioawfh askjh dfkjsdahf kasjdfh sa</p>
<p>asdfsadf asikfdj sadlkf jasilkfr ioawfh askjh dfkjsdahf kasjdfh sa</p>
<p>asdfsadf asikfdj sadlkf jasilkfr ioawfh askjh dfkjsdahf kasjdfh sa</p>
<p>asdfsadf asikfdj sadlkf jasilkfr ioawfh askjh dfkjsdahf kasjdfh sa</p>
<p>asdfsadf asikfdj sadlkf jasilkfr ioawfh askjh dfkjsdahf kasjdfh sa</p>
<p>asdfsadf asikfdj sadlkf jasilkfr ioawfh askjh dfkjsdahf kasjdfh sa</p>
<p>asdfsadf asikfdj sadlkf jasilkfr ioawfh askjh dfkjsdahf kasjdfh sa</p>
<p>asdfsadf asikfdj sadlkf jasilkfr ioawfh askjh dfkjsdahf kasjdfh sa</p>
<p>asdfsadf asikfdj sadlkf jasilkfr ioawfh askjh dfkjsdahf kasjdfh sa</p>
<p>asdfsadf asikfdj sadlkf jasilkfr ioawfh askjh dfkjsdahf kasjdfh sa</p>
<p>asdfsadf asikfdj sadlkf jasilkfr ioawfh askjh dfkjsdahf kasjdfh sa</p>
<p>asdfsadf asikfdj sadlkf jasilkfr ioawfh askjh dfkjsdahf kasjdfh sa</p>
<p>asdfsadf asikfdj sadlkf jasilkfr ioawfh askjh dfkjsdahf kasjdfh sa</p>
<p>asdfsadf asikfdj sadlkf jasilkfr ioawfh askjh dfkjsdahf kasjdfh sa</p>
<p>asdfsadf asikfdj sadlkf jasilkfr ioawfh askjh dfkjsdahf kasjdfh sa</p>
<p>asdfsadf asikfdj sadlkf jasilkfr ioawfh askjh dfkjsdahf kasjdfh sa</p>
</div>
<div class="foot">Foot</div>
有一个名为“btnTest”的按钮,并为其添加了Form
个事件。
_click
现在,我想使用private void btnTest_Click(object sender, EventArgs e)
{
MessageBox.Show("button click");
}
类为此_click
动态生成另一个button
事件,因此会覆盖现有的Control
,因此我的表单看起来像这样:
event_hendler
所以我的目标是启用 public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
Control[] controls = Controls.Find("btnTest", true); //find control by name
controls[0].Click += Form1_Click; //generate button click
}
private void Form1_Click(object sender, EventArgs e)
{
MessageBox.Show("control click"); //want to be displayed
}
private void btnTest_Click(object sender, EventArgs e)
{
MessageBox.Show("button click"); //don't want to be displayed
}
}
和忽略 Form1_Click
并在代码中动态执行此操作。
完成some research,但无法得到答案。
为什么我这样做?
我的主要目标是使用 ENTER 关键字而不是 TAB 来浏览控件,因此当我偶然发现按钮时,我只想继续前进而不会触发原始事件。< / p>
请注意btnTest_Click
事件在btnTest_Click
事件之前被触发,而错误的解决方案是直接在Form1_Click
中执行某些操作,因为我想要使用的控件数量有限< strong> ENTER 并且帽子是可更改的,因此我想忽略的按钮将位于btnTest_Click
数组中。
任何建议都有帮助,谢谢你的时间。
答案 0 :(得分:1)
您如何看待这种方法
Control control = Controls.Find("btnTest", true).FirstOrDefault(); //find control by name
if(control != null)
{
btnTest.Click -= btnTest_Click; //Remove Default Event Handler.
control.Click += Form1_Click; //generate button click
}
答案 1 :(得分:0)
试试这个:
controls[0].Click += Form1_Click;
controls[0].Click -= btnTest_Click;
答案 2 :(得分:0)
经过一番努力和研究后,我找到了解决方案。
我的逻辑是删除controls[0].Click += Form1_Click;
之前的所有事件hendler,这将是覆盖原始事件的方法。
试过这样的事情
controls[0].Click += null;
controls[0].Click += Form1_Click;
但它不起作用,所以我找到了基于this答案的解决方案,我的论坛看起来像这样:
public Form1()
{
InitializeComponent();
InitializeComponent();
Control[] controls = Controls.Find("btnTest", true); //find control by name
Button btn = controls[0] as Button; //safe cast as Button
RemoveClickEvent(btn);
controls[0].Click += Form1_Click; //generate button click
}
private void Form1_Click(object sender, EventArgs e)
{
MessageBox.Show("control click");
}
private void btnTest_Click(object sender, EventArgs e)
{
MessageBox.Show("button click");
}
//remove all event hendlers for button
private void RemoveClickEvent(Button b)
{
FieldInfo f1 = typeof(Control).GetField("EventClick",
BindingFlags.Static | BindingFlags.NonPublic);
object obj = f1.GetValue(b);
PropertyInfo pi = b.GetType().GetProperty("Events",
BindingFlags.NonPublic | BindingFlags.Instance);
EventHandlerList list = (EventHandlerList)pi.GetValue(b, null);
list.RemoveHandler(obj, list[obj]);
}