如何从类中引用控件 [英] how to take referance to a control from a class

查看:63
本文介绍了如何从类中引用控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我有一个名为Thumbnail的类,它需要一个成员变量,该成员变量需要引用listview控件,以便进行编码..

thnx

hi all ,

I have a class called Thumbnail which needs a member variable that needs to take a referance to a listview control so how to code it ..

thnx

推荐答案

我要假设您需要访问一个存在于某个实例上的ListView控件实例.在ThumbNail类的实例内的表单(如果ThumbNail是静态类,这似乎是最不可能的,请告诉我).

答案部分取决于创建ThumbNail类实例的位置以及ListView控件所在的Form实例的位置.

一种策略是让ThumbNail类引发一个Form实例订阅的自定义事件,并且ThumbNail类中的此事件传递更新(或以其他方式)Form上的ListView所需的信息.
如果是在窗体中的代码创建ThumbNail类的实例的情况下,则可以在类型为ListView的ThumbNail类中定义一个公共字段,并且在创建ThumbNail实例时,窗体可以注入"直接引用当前ListView实例.

如果此处仅涉及一个Form,则需要访问ThumbNail实例:您还可以创建类型为ListView的公共静态变量,可以在其中插入对创建Form时创建的ListView的引用.可以在Form外部访问...但是根据您使用NameSpaces的方式,这会有所不同.

学习这三种对象之间通信的策略:通过引发事件,通过注入实例以及通过静态属性发布对实例的引用……将真正帮助您解决许多问题.
I''m going to assume that you need to access an instance of a ListView Control that exists on some instance of a Form within instances of your ThumbNail Class (if ThumbNail is a static class, which seems most unlikely, let me know).

The answer partly depends on where the ThumbNail Class instances, and the instance of the Form on which the ListView Control is sited ... are created.

One strategy is to have the ThumbNail Class raise a custom event that the instance of the Form subscribes to, and this event in the ThumbNail class passes the information needed to update (or whatever) the ListView on the Form.

If it''s the case that the code in the Form creates the instances of the ThumbNail classes, then you can define a public Field in the ThumbNail class of type ListView, and the Form, while creating the ThumbNail instance, can "inject" a direct reference to the current ListView instance into it.

If there''s only one Form involved here the ThumbNail instances need to access: you can also create a public static variable of type ListView into which you can insert the reference to the ListView created when the Form is created, and that can then be accessed outside the Form ... but depending on how you use NameSpaces, this will vary.

Learning these three strategies for communication between objects: by raising events, by injection of instances, and by publishing references to instances via static properties ... will really help you solve many problems.


这篇关于如何从类中引用控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆