问题描述
我知道如何查找和收集在一个Windows窗体中使用的所有控件的列表。
事情是这样的:
I know how to find and collect a list of all the controls used in a Windows Form.Something like this:
static public void FillControls(Control control, List<Control> AllControls)
{
String controlName = "";
controlName = control.Name;
foreach (Control c in control.Controls)
{
controlName = c.Name;
if ((control.Controls.Count > 0))
{
AllControls.Add(c);
FillControls(c, AllControls);
}
}
}
不过这个功能不检索在诸如表单底部的非可视组件HelpProvider,ImageList中,TableAdapter的,数据集等。
However this function does not retrieve the non-visual components on the bottom of the form like the HelpProvider, ImageList, TableAdapters, DataSets, etc.
有没有办法让这些组件的名单?以及
Is there a way to get the list of these components as well?
编辑:
感谢@HighCore指着我使用系统.ComponentModel.Component而不是在一个类似的功能确实让我的清单与组件,例如将ImageList,帮助供应商和BindingSource的。
但是,我还是从这个名单错过的TableAdapter和数据集。我想,因为那些直接从object继承。
Thanks @HighCore for pointing me to use System.ComponentModel.Component instead in a similar function does get me a list with components such the ImageList, the Help Provider and the BindingSource.However, I still miss from this list the TableAdapters and the DataSets. I suppose because those inherit directly from Object.
请。不要参考我的旧帖子,显示了类似的功能,矿山,只有得到控制列表。
Please. Don't refer me to older posts which shows a similar function to mine and that only gets the list of the controls.
编辑:为什么投反对票?这个问题从来没有被回答过了!
Why the negative votes? This question has never been answered before!
推荐答案
令人惊讶,似乎要做到这一点是通过反射的唯一途径。
Surprisingly, it seems the only way to do this is via reflection.
private IEnumerable<Component> EnumerateComponents()
{
return from field in GetType().GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)
where typeof (Component).IsAssignableFrom(field.FieldType)
let component = (Component) field.GetValue(this)
where component != null
select component;
}
这篇关于查找Windows窗体C#组件(未控制)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!