当选择另一个控件时,WinForms 控件事件处理程序将被删除,因此它变为“未引用”;
有关 WinForms 设计器以及如何自定义行为的问题。我多次看到的是,当您为按钮选择不同的事件处理程序时,它会在旧事件处理程序不再使用时删除旧事件处理程序(如代码中所示)。
我想避免这种行为,但找不到为此的配置。有人给点提示吗?谢谢!
更新 由于多个评论首先质疑触发此操作的操作,我想指出它主要是在重构现有代码库期间给我带来的打击。
Question about the WinForms designer and how to customize behavior. What I've seen multiple times is that when you select a different event handler for a button it will remove the old one (as in ,the code) when it becomes unused.
I want to avoid this behavior but can't find configuration for this. Anyone a hint? Thanks!
Update
Since multiple comments question the actions that trigger this in the first place, I'd like to point out that it has mostly hit me during refactoring of an existing code base.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
没有这方面的配置。设计者做了正确的事情,它只删除了没有代码的事件处理程序。一旦您将某些内容放入方法主体中,它就会保留您编写的内容并生成一个新方法。这可以确保您不会丢失代码并且确保您的代码中不会出现死方法。
请注意,在同一类(窗体)中为控件的事件添加多个事件处理程序没有什么意义。您应该只合并处理程序的代码。这也确保了您不会有任何意外,同一事件的多个订阅者运行的顺序是相当不可预测的。设计器仅支持单个事件处理程序,只是因为它没有任何方法来跟踪多个事件处理程序。
There is no configuration for this. The designer does the Right Thing, it only removes event handlers that have no code. As soon as you put something in the method body then it preserves what you've written and generates a new method. This ensures that you don't lose code and ensures that you don't have dead methods littering your code.
Beware that adding more than one event handler for a control's event in the same class (form) makes very little sense. You should just merge the code of the handlers. This also ensures that you won't have any surprises, the order in which multiple subscribers for the same event runs is fairly unpredictable. The designer only supports a single event handler, simply because it doesn't have any way to track more than one.
这就是设计器的工作方式 - 您无法更改它。
要解决您的问题,您可以做的就是在代码中添加事件处理程序,而不是在设计器中:
我必须指出,我质疑是否需要两个单独的事件处理程序。
This is just the way the Designer works - you can't change it.
What you can do to work around your problem is to add your event handlers in code, rather than in the designer:
I must point out that I question the need for two separate event handlers.