TreeNode.EndEdit 与 NodeLabelEditEventArgs.CancelEdit
TreeNode.EndEdit
和设置 NodeLabelEditEventArgs.CancelEdit
有什么区别?
What is the difference between TreeNode.EndEdit
and setting NodeLabelEditEventArgs.CancelEdit
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,从表面上看,调用
EndEdit(true)
确实与在 AfterLabelEdit 或 BeforeLabelEdit 事件处理程序。然而,这两种方法并不等同,并且不用于相同的目的。最好用一个实际的行为示例来演示:
它们做同样的事情,因为:
EndEdit(true)
,树节点将离开编辑模式并放弃更改,e .CancelEdit = true
在AfterLabelEdit
期间,树节点将离开编辑模式并放弃更改。但它们并不等同,因为:
EndEdit(true)
,树节点编辑模式将不会改变(显然),AfterLabelEdit
期间不发出e.CancelEdit = true
,树节点将仍然离开编辑模式(并提交更改),BeforeLabelEdit
期间不发出e.CancelEdit = true
,树节点将 >仍然进入编辑模式。另一个区别是
EndEdit()
触发AfterLabelEdit
,但AfterLabelEdit
不会递归地触发自身(幸运的是)。现在,通常 NodeLabelEditEventArgs.CancelEdit 用于验证,即丢弃对树节点标签的无效修改(在
AfterLabelEdit
期间)或完全阻止用户编辑某些节点的标签(在AfterLabelEdit
期间) >BeforeLabelEdit)。在这两种情况下,用户要么已经完成标签编辑,要么尚未开始编辑标签。EndEdit() 应该是用于在用户仍在编辑标签时强制提交或取消编辑。当然,这意味着
EndEdit(false)
是邪恶且错误,因为它在之前提交了潜在的破坏性更改用户已完成输入并且未经他的同意。所以,实际上我从来没有见过这样称呼它。如果有其他事情绝对需要当前的焦点,而您又不想这样做,则调用
EndEdit(true)
放弃当前编辑可能会很有用当树节点失去焦点时自动提交不完整的编辑。不过,以这种方式丢弃用户的工作仍然是粗鲁的。回到 MFC 时代,
EndEdit(true)
等效项有一个合法用途:当用户按下ESC
键时取消编辑。不管你相信与否,当时 MFC 中不支持开箱即用的功能(也许今天仍然不支持,我没有检查)。但现在,我认为
EndEdit()
已经没有多少用处了。最好让它安息。Well, on the surface, calling
EndEdit(true)
indeed appears to do the same thing as issuinge.CancelEdit = true
in an AfterLabelEdit or BeforeLabelEdit event handler. However, the two approaches are not equivalent, and are not used for the same purpose.Best to demonstrate with an actual behavior example:
They do the same thing, because:
EndEdit(true)
, the tree node will leave edit mode and discard changes,e.CancelEdit = true
duringAfterLabelEdit
, the tree node will leave edit mode and discard changes.But they're not equivalent, because:
EndEdit(true)
, the tree node edit mode will not change (obviously),e.CancelEdit = true
duringAfterLabelEdit
, the tree node will still leave edit mode (and commit changes),e.CancelEdit = true
duringBeforeLabelEdit
, the tree node will still enter edit mode.Another difference is that
EndEdit()
triggersAfterLabelEdit
, butAfterLabelEdit
doesn't recursively trigger itself (fortunately).Now, usually NodeLabelEditEventArgs.CancelEdit is used for validation, i.e. to discard invalid modifications to the tree node label (during
AfterLabelEdit
) or to completely prevent the user from editing the labels of some nodes (duringBeforeLabelEdit
). In both cases, the user has either already finished or has not yet started editing the label.EndEdit() is supposed to be used to forcibly commit or cancel editing while the user is still editing the label. Of course, that means
EndEdit(false)
is evil and wrong, because it commits a potentially breaking change before the user has finished typing and without his consent. So, I've never seen it called that way, actually.Calling
EndEdit(true)
to discard the current edit might be useful if something else absolutely requires the focus right now and you don't want to autocommit the incomplete edit when the tree node loses focus. Throwing away your user's work that way is still rude, though.Back in the MFC days, there was a legitimate use for the
EndEdit(true)
equivalent: cancel editing when the user pressed theESC
key. Believe it or not, that feature wasn't supported out of the box in MFC at that time (and maybe still isn't today, I didn't check).But nowadays, there's not much use left for
EndEdit()
in my opinion. Better let it rest in peace.