反射获取窗体所有控件的Text
2014-03-17 16:33
281 查看
可以直接通过反射获取当前窗体的所有控件的Text(具有Text属性),具体代码如下:
代码
foreach (var field in form.GetType().GetFields(System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public)) { string fieldValue = ""; try { PropertyInfo proText = field.FieldType.GetProperty("Text"); if (field.FieldType == typeof(System.Windows.Forms.Label) || field.FieldType == typeof(DevComponents.DotNetBar.LabelX) ) { fieldValue = proText.GetValue(field.GetValue(form), null).ToString(); } else if (field.FieldType == typeof(System.Windows.Forms.Button) || field.FieldType == typeof(DevComponents.DotNetBar.ButtonX) || field.FieldType == typeof(GPOS.Controls.ButtonNew) ) { fieldValue = proText.GetValue(field.GetValue(form), null).ToString(); } else if (field.FieldType == typeof(DevComponents.DotNetBar.ButtonItem) || //field.FieldType == typeof(DevComponents.DotNetBar.TextBoxItem) || field.FieldType == typeof(DevComponents.DotNetBar.LabelItem) ) { fieldValue = proText.GetValue(field.GetValue(form), null).ToString(); } else if (field.FieldType == typeof(System.Windows.Forms.ToolStripMenuItem) ) { fieldValue = proText.GetValue(field.GetValue(form), null).ToString(); } else if (field.FieldType == typeof(System.Windows.Forms.ToolStripButton) ) { // fieldValue = proText.GetValue(field.GetValue(form), null).ToString(); PropertyInfo proToolTipText = field.FieldType.GetProperty("ToolTipText"); fieldValue = proToolTipText.GetValue(field.GetValue(form), null).ToString(); } else if (field.FieldType == typeof(System.Windows.Forms.CheckBox) || field.FieldType == typeof(DevComponents.DotNetBar.Controls.CheckBoxX) ) { fieldValue = proText.GetValue(field.GetValue(form), null).ToString(); } else if (field.FieldType == typeof(System.Windows.Forms.DataGridViewTextBoxColumn) || field.FieldType == typeof(System.Windows.Forms.DataGridViewCheckBoxColumn) ) { PropertyInfo proHeaderText = field.FieldType.GetProperty("HeaderText"); fieldValue = proHeaderText.GetValue(field.GetValue(form), null).ToString(); } else { continue; } } catch { } }
代码
相关文章推荐
- 反射设置当前窗体所有控件的Text
- 获取窗体上的所有控件
- 获取窗体上的所有控件
- C#通过反射获取当前工程中所有窗体并打开的方法
- 循环遍历获取窗体或页面所有同类型的控件
- JS获取页面input控件中所有text控件并追加样式属性
- JS获取页面input控件中所有text控件并追加样式属性
- js 获取asp 控件DropDownList的选择值,及所有的text和value
- jquery对所有<input type="text"的控件赋值
- jquery获取select,option所有的value和text的实例
- VC获取Excel所有工作表名,并将其显示到一个CListCtrl控件中
- 【.Net码农】C#反射获取 所有字段 及 私有字段
- 【转】C#实现窗体中所有控件跟随窗体尺寸的自由变换
- 获取IE (控件)的所有链接(包括Frameset, iframe)
- 如何获取WM当前窗口下的所有控件?
- 获取控件所有事件列表
- 使用反射将业务对象绑定到 ASP.NET 窗体控件
- WinForm中使用反射将业务对象绑定到窗体或控件容器
- 获取控件的值并进行窗体间的传递
- Java中用反射的方式获取类中的所有属性和方法,以及获取属性的默认值,设置属性的默认值