如何通过代码获知Form上有哪些控件?

sarmoo 2003-01-09 05:09:47
例如:一个自制控件,需要使用Form的ImageList,但怎样才能取到Form的这个ImageList?
...全文
93 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
project 2003-01-10
  • 打赏
  • 举报
回复
啊!来晚了。做一个ImageList的属性是正确的方法。
sarmoo 2003-01-10
  • 打赏
  • 举报
回复
OK!我试了一下,只要设置了这个属性,当Form中增加ImageList时,控件的这个属性的列表就会自动把这个新的ImageList增加进去。
原来这么简单,我把它想的太复杂了。
谢谢各位的回答!
准备结贴!
TheAres 2003-01-09
  • 打赏
  • 举报
回复
To sarmoo (Echo):

ImageList并不是继承自Control,所以通过Controls来判断根本方向不对.

你可以在你的控件中定义一个 MyImageList属性,来在Form中创建这个控件的时候,对这个属性赋值.

public class MyControl
{
private ImageList _imageList;
public MyImageList
{
set { _imageList = value;} //你也可以在这里写其他的处理
get { return _imageList;}
}
}

使用的时候,


MyControl c = new MyControl();
c.MyImageList = this.imageList1; //this是指Form

ssdjmcj8048 2003-01-09
  • 打赏
  • 举报
回复
try 判断一下不就可以了,其实不光有Component的问题,Com控件也有问题。
sarmoo 2003-01-09
  • 打赏
  • 举报
回复
当然,这个是在我的 class MyControl:Control内的一个属性。
sarmoo 2003-01-09
  • 打赏
  • 举报
回复
public ImageList ImageList
{
get
{
foreach(ImageList item in this.Parent.Controls)
{
if(item!=null)
imageList=item;
}
imageList;
}

}
我改写了,不过总觉得似乎有问题…………
sheep2002 2003-01-09
  • 打赏
  • 举报
回复
private ImageList _image=new ImageList();
public ImageList Image
{
set
{
_image=value;
}
get
{
return _image;
}
}
sarmoo 2003-01-09
  • 打赏
  • 举报
回复
系统提示:
Cannot convert type 'System.Windows.Forms.Control' to 'System.Windows.Forms.ImageList'

呵呵!
poetc 2003-01-09
  • 打赏
  • 举报
回复

foreach(Control item in this.controls)
{
if(item.GetType==(new ImageList()).GetType())
//...
}
henryfan1 2003-01-09
  • 打赏
  • 举报
回复
foreach(Control item in this.controls)
{
if(item.name=="ImageList1")
userControl.image=(ImageList)item
}

110,533

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧