为什么 GotFocus 事件不断从 Designer 中删除自身? [英] Why does GotFocus event keep deleting itself from Designer?
问题描述
我想将 GotFocus 事件添加到 Windows 窗体文本框,因此我使用了 这个问题;它有效,但在我运行我的应用程序几次后,这段代码会自行删除,我不知道为什么.
I wanted to add a GotFocus event to a Windows Forms Textbox, so I used the method described in this question; it works, but after I run my application a couple of times the piece of code deletes itself and I don't know why.
这是不断删除自己的代码:
This is the code that keeps deleting itself:
txtID.GotFocus += txtID_GotFocus;
推荐答案
它会消失,因为您在添加事件处理程序时没有使用 WinForms 设计器使用的约定.
It disappears because you don't use conventions that are used by WinForms designer when you add event handlers.
使用 GotFocus
或 Enter
事件都没有关系.如果您(在 Designer.cs 中)以这种方式手动添加事件处理程序:
It doesn't matter whether you use GotFocus
or Enter
event. If you (in your Designer.cs) manually add event handler this way:
txtID.Enter += txtID_Enter;
那么下次您在设计器界面上移动控件时,它总是会从设计器中消失.
then it would always disappear from designer next time you move control on designer surface.
您必须以这种方式添加事件处理程序:
txtID.GotFocus += new System.EventHandler(txtID_Focus);
txtID.Enter += new System.EventHandler(txtID_Enter);
并且没有任何东西会消失,因为这是设计师期望代码的方式.
and nothing would disappear because it's the way designer expects code to be.
这篇关于为什么 GotFocus 事件不断从 Designer 中删除自身?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!