[英]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.