以编程方式调用按钮的单击方法

时间:2022-01-23 18:09:19

Simple problem (I think): I want to be able to invoke a click method on a predefined object, specifically, the bindingNavigatorDeleteItem button on the standard c# BindingNavigator. I need to intercept the delete so that I can verify that the record is allowed to be deleted. If it is, I want to invoke the aforementioned click event which does a nice job of deleting said record. If the record is not eligible for deletion, I want to abort the delete.

简单的问题(我认为):我希望能够在预定义的对象上调用click方法,特别是标准c#BindingNavigator上的bindingNavigatorDeleteItem按钮。我需要拦截删除,以便我可以验证是否允许删除该记录。如果是,我想调用前面提到的点击事件,它可以很好地删除所述记录。如果记录不符合删除条件,我想中止删除。

An engineering colleague of mine suggests that I simply add another button to the toolstrip and use it's click method (which, of course, I can get to) to check the records eligibility and call the original delete button as needed.

我的一位工程同事建议我只是在工具条中添加另一个按钮并使用它的点击方法(当然,我可以到达)检查记录资格并根据需要调用原始删除按钮。

If there is another, better way, please pass it along.

如果有另一种更好的方法,请传递它。

2 个解决方案

#1


To simply invoke the click - you should be able to use PerformClick().

要简单地调用单击 - 您应该能够使用PerformClick()。

I was a little lost by the other things you mentioned - can you clarify?

你提到的其他事情我有点迷失了 - 你能澄清一下吗?

#2


Instead of intercepting the toolstrip button (and leaving other techniques open to the user, such as deleting a row within a DataGrid), use one of the events on your Data Source to cancel the delete if invalid.

而不是拦截工具条按钮(并保留对用户开放的其他技术,例如删除DataGrid中的行),使用数据源上的某个事件取消删除(如果无效)。

#1


To simply invoke the click - you should be able to use PerformClick().

要简单地调用单击 - 您应该能够使用PerformClick()。

I was a little lost by the other things you mentioned - can you clarify?

你提到的其他事情我有点迷失了 - 你能澄清一下吗?

#2


Instead of intercepting the toolstrip button (and leaving other techniques open to the user, such as deleting a row within a DataGrid), use one of the events on your Data Source to cancel the delete if invalid.

而不是拦截工具条按钮(并保留对用户开放的其他技术,例如删除DataGrid中的行),使用数据源上的某个事件取消删除(如果无效)。