如何运行用户控件不定期的事件
本文关键字:控件 不定期 事件 用户 何运行 运行 | 更新日期: 2023-09-27 17:58:03
我有一个带有一些按钮的用户控件(tmNewItem、tmEdit、tmInsert)
我为他们写了一个clickButton事件。
例如:
public void btnEdit_Click(object sender, EventArgs e)
{
btnNew.Enabled = false;
btnEdit.Enabled = false;
}
我在另一个项目中使用了这个用户控件,并为按钮编写了另一种方法,并将其分配给usr控件:
public void DTedit(object sender, EventArgs e)
{
}
private void UserControl_Load(object sender, EventArgs e)
{
DT_Navigator.btnCancel.Click += new EventHandler(DTedit);
}
现在,当我运行该项目并按下btnEdit按钮时,第一次,btnEdit_Click将执行,然后执行DTedit。我能换一下吗?我的意思是DTedit(我在项目中定义它)第一次运行,然后btnEdit_Click(我在用户控件中定义它的)运行?我该怎么做?
试试这个
public void DTedit(object sender, EventArgs e)
{
//Place your code here
DT_Navigator.btnCancel.Click -= new EventHandler(DTedit); //This will remove handler from the button click and it will not be executed next time.
}
private void UserControl_Load(object sender, EventArgs e)
{
DT_Navigator.btnCancel.Click += new EventHandler(DTedit);
}
建议代码
//User control
public event CancelEventHandler BeginEdit;
public event EventHandler EndEdit;
private btnYourButton_Click(object sender, EventArgs e)
{
CancelEventArgs e = new CancelEventArgs();
e.Cancel = false;
if (BeginEdit != null)
BeginEdit(this, e);
if (e.Cancel == false)
{
if (EndEdit != null)
EndEdit(this, new EventArgs);
//You can place your code here to disable controls
}
}