我想要在自定义类中获取主窗口的控件imageList1。
好了 我就直接在自定义类构造函数中这样写到:
public class Class1 {
Form1 m_ref_form;
ImageList imageList1;
public Tab1Click(Form1 in_form)
{
m_ref_form = in_form;
imageList1=m_ref_form.imageList1;
}
自认为能够成功的访问到了,结构报错啦。
好吧 ,我们来看看在主窗口中我拖进去的imageList1控件的声明定义,是私有的啊:
private System.Windows.Forms.ImageList imageList1;
那是不是我在主类中声明一个public 的ImageList,命名为imgL,并将imageList1赋给它,这样就行了?那么修改为:
public class Class1 {
Form1 m_ref_form;
ImageList imageList1;
public Tab1Click(Form1 in_form)
{
m_ref_form = in_form;
imageList1=m_ref_form.imgL;
}
啊 的确可以。但是想想,这样会不会安全性不高。
作为一个初学菜鸟 很多很多不懂 有人有更好的方法吗 求解
访问主窗体控件
本文探讨了如何从自定义类中访问主窗体中的私有控件ImageList。通过将私有ImageList变为公共属性并传递引用的方式,解决了跨类访问的问题。
382

被折叠的 条评论
为什么被折叠?



