父母与子女班

编程入门 行业动态 更新时间:2024-10-15 04:21:38
本文介绍了父母与子女班的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

大家好 希望有人能帮助我. 我有一个主窗体(Form1)实例化一个子类(class1). 在我的Form1上,我有一个Treeview. 我想做的是在子类中触发事件时为树视图更改ImageIndex? 有可能吗? 谢谢:

Hi Guys Hope someone can help me. I have a main Form (Form1) that instantiate a child class (class1). On my Form1 I have a Treeview. What I want to do is to change an ImageIndex for my treeview when an event is triggered in my child class? Is it possible ? Thanks

推荐答案

是的:只需在Form1中为class1实例中的事件添加处理程序即可.当类发出事件信号时,表单可以响应并更新Treeview. Yes: just add a handler in your Form1 for the event in your class1 instance. When the class signals the event, the form can respond and update the Treeview.

另一种处理方法是着重于使Form1中的TreeView可用于class1.由于Form1创建了class1,因此这很容易. 在class1中添加一个类型为TreeView的公共属性: Another way to approach this is to focus on making the TreeView in Form1 available to class1. Since Form1 creates class1, this is easy. In class1 add a public Property of Type TreeView: public TreeView ParentTreeView { get; set; }

在您的代码中Form1实例化class1的位置:将对TreeView的引用插入到class1中:

At the point in your code where Form1 has instantiated class1: insert the reference to the TreeView into class1:

MyClass1 = new class1(); MyClass1.ParentTreeView = Form1.F1TreeView;

现在,您的class1实例可以访问Form1的TreeView直接. 如果某个事件可能有很多订阅者/消费者,请考虑让该类引发一个事件. 如果可能有许多需要访问对象的Class实例,请考虑插入一个对象实例(在这种情况下为TreeView). 在这种特定情况下:更改TreeView的ImageIndex不需要任何其他外部信息.如果需要在其父级中与之交互的任何class1需要许多其他引用,那么使用事件/订阅者模型当然更为合适.

Now, your instance of class1 can access Form1''s TreeView directly. If there are, potentially, many subscribers/consumers to/of an Event, consider having the Class raise an Event. When there are, potentially, many instances of a Class that need access to an Object, consider insertion of an instance of an object (in this case a TreeView). In this specific case: changing the ImageIndex of a TreeView doesn''t require any other external information. If whatever class1 needed to interact with in its parent required many other references, then, of course, using an Event/Subscriber model would be more appropriate.

更多推荐

父母与子女班

本文发布于:2023-10-28 03:58:38,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1535448.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:子女   父母

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!