TreeNode.EndEdit 与 NodeLabelEditEventArgs.CancelEdit

发布于 2024-08-31 16:12:10 字数 94 浏览 2 评论 0原文

TreeNode.EndEdit 和设置 NodeLabelEditEventArgs.CancelEdit 有什么区别?

What is the difference between TreeNode.EndEdit and setting NodeLabelEditEventArgs.CancelEdit?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小糖芽 2024-09-07 16:12:10

好吧,从表面上看,调用 EndEdit(true) 确实与在 AfterLabelEditBeforeLabelEdit 事件处理程序。然而,这两种方法并不等同,并且不用于相同的目的。

最好用一个实际的行为示例来演示:

它们做同样的事情,因为:

  • 如果您调用 EndEdit(true),树节点将离开编辑模式并放弃更改,
  • 如果您发出 e .CancelEdit = trueAfterLabelEdit 期间,树节点将离开编辑模式并放弃更改。

但它们并不等同,因为:

  • 如果您调用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 issuing e.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:

  • If you call EndEdit(true), the tree node will leave edit mode and discard changes,
  • If you issue e.CancelEdit = true during AfterLabelEdit, the tree node will leave edit mode and discard changes.

But they're not equivalent, because:

  • If you don't call EndEdit(true), the tree node edit mode will not change (obviously),
  • If you don't issue e.CancelEdit = true during AfterLabelEdit, the tree node will still leave edit mode (and commit changes),
  • If you don't issue e.CancelEdit = true during BeforeLabelEdit, the tree node will still enter edit mode.

Another difference is that EndEdit() triggers AfterLabelEdit, but AfterLabelEdit 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 (during BeforeLabelEdit). 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 the ESC 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文