在我们编程的时候,有时需要动态的获取当前窗体控件的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. System.TypeLoadException: Could not load type 'System.IO.Compression.CompressionLevel' from assembly 'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.

    1.提示错误信息: zipSystem.TypeLoadException: Could not load type 'System.IO.Compression.CompressionLevel' ...

  2. 错误Log日志的收集

    1.在Application里面初始化 AppCrashHandler.getInstance(this); 2.创建一个类 package com.lvshandian.partylive.util ...

  3. iOS界面-仿网易新闻左侧抽屉式交互 续(添加新闻内容页和评论页手势)

     本文转载至  http://blog.csdn.net/totogo2010/article/details/8637430       1.介绍 有的博友看了上篇博文iOS界面-仿网易新闻左侧抽屉 ...

  4. Altera Quartus 13.1 仿真工具路径错误问题解决 Can't launch the ModelSim-Altera software

    Altera Quartus 13.1 仿真工具路径错误问题解决 Quartus13.1 自带的ModelSim-Altera 10.1d 版本, 在做仿真时调用 ModelSim-Alteara,发 ...

  5. maven snapshot和release版本的区别(转)

    在使用maven过程中,我们在开发阶段经常性的会有很多公共库处于不稳定状态,随时需要修改并发布,可能一天就要发布一次,遇到bug时,甚至一天要发布N次.我们知道,maven的依赖管理是基于版本管理的, ...

  6. 我的Android进阶之旅------>Android检测wifi连接状态

    今天要实现监听系统Wifi连接状态,下面代码简化后提取出来的,以备后用. step1. 编写BroadcastReceiver import android.content.BroadcastRece ...

  7. 通过JMX获取weblogic的监控指标

    通过JMX获取weblogic的监控数据,包括JDBC,SESSION,SERVERLET,JVM等信息.主要用到weblogic自己的t3协议,所以要用到weblogic的jar包:wlfullcl ...

  8. NiFi汉化

    ①在源文件中的 source-nifi/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-web/nifi-web-ui/src中修 ...

  9. spring-boot5代码

    App.java package com.kfit.spring_boot_mybatis; import org.mybatis.spring.annotation.MapperScan; impo ...

  10. iPad actionsjeet

    在iphone和ipad上使用UIActionShee控件t的效果会不一样,在苹果的官方文档中有相关说明: 在ipad上使用UIActionSheet控件改控件不再从底部弹出,而是从屏幕中间弹出与UI ...