将节点添加到 C# TreeView 控件时如何运行代码
当将新节点添加到树视图控件时,我需要运行代码。当将新节点添加到 TreeView 时,控件附带的 Control_Added 事件不会被触发,这很奇怪,因为我期望它是一个控件,我的意思是一个节点。为此应该使用什么事件?
谢谢..
I need to run code when a new node is added to a treeview control. Control_Added event that comes with the control does not get fired when a new node is added to the TreeView which is weird as I expected that it was a control, a node I mean. What event should be used for this?
Thanks..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
没有为此举办的活动。这并不罕见,事件旨在告诉您的代码发生了一些您感兴趣的事情,一些您不知道的事情。您的程序的用户无法向树视图添加节点,只有您的代码可以做到这一点。你已经知道了。
解决方法是从 TreeView 继承一个类并添加所需的事件,以及添加节点并引发事件的公共帮助器方法。这不是一个很好的后门,因为仍然有一个后门可以添加您无法轻易关闭的节点。
There is no event for that. This is not unusual, events are meant to tell your code that something of interest happened, something you don't otherwise know about. The user of your program cannot add a node to the treeview, only your code can do that. You already know about it.
Inheriting a class from TreeView and adding the event you want, as well as a public helper method that adds the node and raises the event is the workaround. It isn't a very good one because there is still a backdoor to add nodes that you cannot easily close.