可以直接通过反射获取当前窗体的所有控件的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的更多相关文章

  1. 反射设置当前窗体所有控件的Text

    在我们编程的时候,有时需要动态的获取当前窗体控件的Text,但是又不能一个一个控件的设置,这个时候可以通过反射来动态设置. 第一步:先建立一个类来保存控件的Text信息. public class C ...

  2. C# WPF 获取窗体和控件的句柄

    窗体: IntPtr hwnd = new WindowInteropHelper(this).Handle; 控件: IntPtr hwnd = ((HwndSource)PresentationS ...

  3. [WinForm] 使用反射将业务对象绑定到窗体或控件容器

    在WebForm中,可以使用反射将业务对象绑定到 ASP.NET 窗体控件.最近做Winform项目,也参考WebForm中的代码实现同样的功能.     Winform没有提供类似WebForm中的 ...

  4. Winform 获取当前单击的控件名称 和 向窗体添加控件

    Winform如何获取当前单击的控件名称,比如有100个Button 和一个button_Click()的按钮事件 ,分别点击不同按钮后显示所点击的按钮名称?private void button_C ...

  5. 最佳实践扩展Windows窗体DataGridView控件 .net 4.5 附示例代码

    Windows窗体DataGridView控件的性能调优.net 4.5   在处理大量数据时, DataGridView 控制可以消耗大量的内存开销,除非你仔细地使用它. 在客户有限的内存,你可以避 ...

  6. C# 委托实例(跨窗体操作控件)

    在C#里面却是可以不用自定义消息这么复杂的方法来实现跨窗体调用控件,C#有更好的办法就是委托. 效果描述:有两个窗体,FORM1(一个名为“打开form2”的button控件)和FORM2(一个名为“ ...

  7. 实现虚拟模式的动态数据加载Windows窗体DataGridView控件 .net 4.5 (一)

    实现虚拟模式的即时数据加载Windows窗体DataGridView控件 .net 4.5 原文地址 :http://msdn.microsoft.com/en-us/library/ms171624 ...

  8. 在C#中子线程如何操作主线程中窗体上控件

    在C#中,直接在子线程中对窗体上的控件操作是会出现异常,这是由于子线程和运行窗体的线程是不同的空间,因此想要在子线程来操作窗体上的控件,是不可能 简单的通过控件对象名来操作,但不是说不能进行操作,微软 ...

  9. C#跨窗体调用控件(委托回调函数使用例子)

    问题: 有两个窗体,FORM1(含一个label控件,一个名为显示form2的button控件)和FORM2(含一个button控件).启动时,FORM1中点击button控件显示form2使FORM ...

随机推荐

  1. 【BZOJ1097】[POI2007]旅游景点atr 最短路+状压DP

    [BZOJ1097][POI2007]旅游景点atr Description FGD想从成都去上海旅游.在旅途中他希望经过一些城市并在那里欣赏风景,品尝风味小吃或者做其他的有趣的事情.经过这些城市的顺 ...

  2. EasyNVR无插件IPC摄像机直播方案前端构建之:如何区分PC端和移动端

    EasyNVR前端为了更好的用户体验,不仅仅设有PC客户端,还适应移动客户端: EasyNVR的客户端中PC端和移动端差异有很多.例如: 由于PC端.移动端自身硬件的差异,所需要展示的样式也会存在一定 ...

  3. Activity设置style透明后与SurfaceView合用引发的无形命案

    近期搞视频通话,SurfaceView是不可缺少的,因为启动视频要载入一些资源,比較耗时.会有1.2s黑屏的现象,为了改善用户体验,我们须要设置Activity的Theme为透明风格(QQ 也是如此) ...

  4. 通过主机名来获取一个ip对象

    //通过名称(ip字符串or主机名)来获取一个ip对象. InetAddress ip = InetAddress.getByName("www.baidu.com");//jav ...

  5. ABAP excel操作 OLE 常用方法和属性

    转自 http://bstone.blog.163.com/blog/static/176820446201172834149199/#userconsent# OLE 常用方法和属性 1.ole中如 ...

  6. Java7、Java8 安装卸载问题

    win7 系统,同时安装了JDK7和JDK8,卸载了JDK8之后,cmd命令行输入:java -version ,本以为显示java版本1.7,结果弹错:has value '1.7',but '1. ...

  7. 【博弈论】hihocoder

    #1163 : 博弈游戏·Nim游戏 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 今天我们要认识一对新朋友,Alice与Bob. Alice与Bob总是在进行各种各样 ...

  8. zabbix 上 mysql 优化

    摘自: https://segmentfault.com/a/1190000001638101

  9. JavaScript多态

    function Master(){ //给动物喂食 this.feed=function(animal,food){ window.alert(animal.constructor); docume ...

  10. css(3)

    行内元素和块内元素可以通过定义display的属性值进行相互转换. 想要叫ul中的li实现横向显示可以在li中加入布局"float:left": 如: /*这个用于控制单个图片区域 ...