大家好 希望有人能帮助我. 我有一个主窗体(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.
更多推荐
父母与子女班
发布评论