在我们编程的时候,有时需要动态的获取当前窗体控件的Text,但是又不能一个一个控件的设置,这个时候可以通过反射来动态设置。

第一步:先建立一个类来保存控件的Text信息。

public class ControlInfo
{
private string name;
private string text;
public string Name
{
get
{
return name;
}
set
{
name = value;
}
}
public string Text
{
get
{
return text;
}
set
{
text = value;
}
}
}

控件信息类

第二步:建立一个存储这些控件的变量。

public List<ControlInfo> LstControlInfo;

第三步:从外部获取控件信息存储到LstControlInfo中。

第四步:通过发射将LstControlInfo里的控件Text设置到form中。

以下是保存的具体代码:

foreach (var field in form.GetType().GetFields(System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public))
{
if (LstControlInfo.Exists(m => m.Name == field.Name))
{
ControlInfo control = LstControlInfo.Find(m => m.Name == field.Name);
PropertyInfo proText = field.FieldType.GetProperty("Text");
if (field.FieldType == typeof(System.Windows.Forms.Label) ||
field.FieldType == typeof(DevComponents.DotNetBar.LabelX)
)
{
proText.SetValue(field.GetValue(form), control.Text, null);
}
else if (field.FieldType == typeof(System.Windows.Forms.Button) ||
field.FieldType == typeof(DevComponents.DotNetBar.ButtonX) ||
field.FieldType == typeof(GPOS.Controls.ButtonNew)
)
{
proText.SetValue(field.GetValue(form), control.Text, null);
}
else if (field.FieldType == typeof(DevComponents.DotNetBar.ButtonItem) ||
//field.FieldType == typeof(DevComponents.DotNetBar.TextBoxItem) ||
field.FieldType == typeof(DevComponents.DotNetBar.LabelItem)
)
{
proText.SetValue(field.GetValue(form), control.Text, null);
}
else if (field.FieldType == typeof(System.Windows.Forms.ToolStripMenuItem)
)
{
proText.SetValue(field.GetValue(form), control.Text, null);
}
else if (field.FieldType == typeof(System.Windows.Forms.ToolStripButton)
)
{
proText.SetValue(field.GetValue(form), control.Text, null);
PropertyInfo proToolTipText = field.FieldType.GetProperty("ToolTipText");
proToolTipText.SetValue(field.GetValue(form), control.Text, null);
}
else if (field.FieldType == typeof(System.Windows.Forms.CheckBox) ||
field.FieldType == typeof(DevComponents.DotNetBar.Controls.CheckBoxX)
)
{
proText.SetValue(field.GetValue(form), control.Text, null);
}
else if (field.FieldType == typeof(System.Windows.Forms.DataGridViewTextBoxColumn) ||
field.FieldType == typeof(System.Windows.Forms.DataGridViewCheckBoxColumn)
)
{
PropertyInfo proHeaderText = field.FieldType.GetProperty("HeaderText");
proHeaderText.SetValue(field.GetValue(form), control.Text, null);
}
}
}

具体设置代码

反射设置当前窗体所有控件的Text的更多相关文章

  1. 反射获取窗体所有控件的Text

    可以直接通过反射获取当前窗体的所有控件的Text(具有Text属性),具体代码如下: foreach (var field in form.GetType().GetFields(System.Ref ...

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

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

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

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

  4. Winform中怎样跨窗体获取另一窗体的控件对象

    场景 Winform中实现跨窗体获取ZedGraph的ZedGraphControl控件对象: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/de ...

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

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

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

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

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

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

  8. WinForm窗体及其控件的自适应

    3步骤: 1.在需要自适应的Form中实例化全局变量   AutoSizeFormClass.cs源码在下方 AutoSizeFormClass asc = new AutoSizeFormClass ...

  9. C#遍历窗体所有控件或某类型所有控件

    //遍历窗体所有控件, foreach (Control control in this.Controls) { //遍历后的操作... control.Enabled = false; } 遍历某个 ...

随机推荐

  1. 一个中断服务子程序ISR

    请看下面的程序(一个中断服务子程序ISR),请指出这段代码的错误.)[中国台湾某著名CPU生产公司2005年面试题] 答案:(1)ISR不能返回一个值.如果你不懂这个,那么是不会被雇用的.(2)ISR ...

  2. 【BZOJ3544】[ONTAK2010]Creative Accounting 前缀和+set

    [BZOJ3544][ONTAK2010]Creative Accounting Description 给定一个长度为N的数组a和M,求一个区间[l,r],使得(\sum_{i=l}^{r}{a_i ...

  3. python网络爬虫之requests库 二

    前面一篇在介绍request登录CSDN网站的时候,是采用的固定cookie的方式,也就是先通过抓包的方式得到cookie值,然后将cookie值加在发送的数据包中发送到服务器进行认证. 就好比获取如 ...

  4. 【docker】kubernetes集群一键部署包

    背景说明: 随着docker使用的逐步深入,docker的管理变得越来越麻烦,单纯的通过docker命令行的方式进行管理已经不能满足需求,同时也存在效率低下的问题.所以急需一个docker集群管理工具 ...

  5. [IR课程笔记]Hyperlink-Induced Topic Search(HITS)

    两个假设 1. 好的hub pages: 好的对某个主题的hub pages 链接许多好的这个主题的authoritative pages. 2. 好的authoritative pages: 好的对 ...

  6. API的理解和使用——列表类型的命令

    列表类型的命令及对应的时间复杂度 操作 命令 功能 时间复杂度 添加 rpush key value [value ...] 向右插入 O(k),k是元素个数 lpush key value [val ...

  7. CountDownLatch,CyclicBarrier,Semaphore的使用

    什么时候使用CountDownLatch CountDownLatch原理和示例 Semaphore信号量的原理和示例 CyclicBarrier的用法 CyclicBarrier 和 CountDo ...

  8. 更新TP-LINK路由器的外网IP到花生壳动态IP解析

    ------------------------------------------------------------------------------- 以下内容可能还是存在问题,等之后有时间再 ...

  9. nginx负载均衡设置

    在nginx的配置文件[./conf/nginx.conf]中加入: upstream test.balance123.com { server 192.168.1.223:80; server 19 ...

  10. 简单通俗解释内外网IP与端口映射

    IP:分为外网IP和内网IP 也就是我们说的外网IP属于实体IP 实体IP,它是独一无二的,在网络的世界里,每一部计算机的都有他的位置,一个 IP 就好似一个门牌!例如,你要去百度的网站的话,就要去『 ...