[英]Adding Items to ListBox, RadioList, Combobox using reflection
[英]Iterating Through ComboBox And Referencing Items Using Reflection
我有一些自定义控件:
1)1用于字符串输入(文本框)
2)1个用于多个字符串输入(组合框)
3)1用于整数输入
页面1进行了一些处理,并且根据用户输入,将任意数量的控件以任何顺序添加到页面2。 由于上述控件的添加以及它们的排序是完全动态的,因此我不得不使用反射功能来引用自定义控件:
例如,如果我想在上述控件之一中引用属性(例如“值”),则使用以下内容
c.GetType().GetProperty("Value")
我的问题如下:
我需要访问包含组合框的控件中的项目 。 通常我会做类似的事情:
foreach(string item in ComboBox1.items){}
通过反射引用控件时,这似乎有些棘手。...有什么想法吗?
注意:如果可能,我想继续使用反射。
你可以试试
foreach (var cntrl in Page2.Controls.OfType<ComboBox>())
{
//
}
要获取该属性,您可以不确定是否要尝试尝试以下操作。
PropertyInfo prop = controlType.GetProperty(propertyName);// Gets Items
MethodInfo methInfo = prop.PropertyType.GetMethod(methodName); // gets the method ('Items.Add')
object obj = prop.GetValue(newControl, null); // gets the current 'Items
methInfo.Invoke(prop.GetValue(newControl, null), new object[] { newValue });
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.