反射设置当前窗体所有控件的Text
在我们编程的时候,有时需要动态的获取当前窗体控件的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的更多相关文章
- 反射获取窗体所有控件的Text
可以直接通过反射获取当前窗体的所有控件的Text(具有Text属性),具体代码如下: foreach (var field in form.GetType().GetFields(System.Ref ...
- [WinForm] 使用反射将业务对象绑定到窗体或控件容器
在WebForm中,可以使用反射将业务对象绑定到 ASP.NET 窗体控件.最近做Winform项目,也参考WebForm中的代码实现同样的功能. Winform没有提供类似WebForm中的 ...
- 最佳实践扩展Windows窗体DataGridView控件 .net 4.5 附示例代码
Windows窗体DataGridView控件的性能调优.net 4.5 在处理大量数据时, DataGridView 控制可以消耗大量的内存开销,除非你仔细地使用它. 在客户有限的内存,你可以避 ...
- Winform中怎样跨窗体获取另一窗体的控件对象
场景 Winform中实现跨窗体获取ZedGraph的ZedGraphControl控件对象: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/de ...
- C# 委托实例(跨窗体操作控件)
在C#里面却是可以不用自定义消息这么复杂的方法来实现跨窗体调用控件,C#有更好的办法就是委托. 效果描述:有两个窗体,FORM1(一个名为“打开form2”的button控件)和FORM2(一个名为“ ...
- Winform 获取当前单击的控件名称 和 向窗体添加控件
Winform如何获取当前单击的控件名称,比如有100个Button 和一个button_Click()的按钮事件 ,分别点击不同按钮后显示所点击的按钮名称?private void button_C ...
- 实现虚拟模式的动态数据加载Windows窗体DataGridView控件 .net 4.5 (一)
实现虚拟模式的即时数据加载Windows窗体DataGridView控件 .net 4.5 原文地址 :http://msdn.microsoft.com/en-us/library/ms171624 ...
- WinForm窗体及其控件的自适应
3步骤: 1.在需要自适应的Form中实例化全局变量 AutoSizeFormClass.cs源码在下方 AutoSizeFormClass asc = new AutoSizeFormClass ...
- C#遍历窗体所有控件或某类型所有控件
//遍历窗体所有控件, foreach (Control control in this.Controls) { //遍历后的操作... control.Enabled = false; } 遍历某个 ...
随机推荐
- 九度OJ 1168:字符串的查找删除 (查找)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:4276 解决:1699 题目描述: 给定一个短字符串(不含空格),再给定若干字符串,在这些字符串中删除所含有的短字符串. 输入: 输入只有1 ...
- iframe式ajax调用
1.新建 a.html <!doctype html> <html> <head> <meta charset='utf-8'> <title&g ...
- 我的Java开发学习之旅------>Java经典排序算法之选择排序
一.算法原理 对比数组中前一个元素跟后一个元素的大小,如果后面的元素比前面的元素小则用一个变量k来记住他的位置, 接着第二次比较,前面"后一个元素"现变成了"前一个元素& ...
- 用php動態產生各種尺寸的圖片
我的做法是用php動態產生各種尺寸的圖片,例如<img src="img.php?src=a.jpg&width=100&height=200"...< ...
- Java for LeetCode 083 Remove Duplicates from Sorted List
Given a sorted linked list, delete all duplicates such that each element appear only once. For examp ...
- 升级pip3的正确姿势
如果你的电脑里装了两个python,就会有两个pip,一个是pip2,一个是pip3,还有可能出现一个既没有2也没有3的pip,一般情况下,pip等于pip2 有时候我们使用pip安装东西会提示我们p ...
- [egret+pomelo]实时游戏杂记(4)
了解了前后端的通信,下面就可以开始自己的业务逻辑了,首先玩家输入名称,选择角色后进入游戏世界. 服务端的demo中已经提供了一些简单的角色信息和属性,数据地址位于 game-server/config ...
- 使用C语言解析URL
1. [代码]容易写成自己输入URL,这里测试一个例子 #include <stdio.h>#include <stdlib.h>#include <string ...
- Log4j 与 logback对比、及使用配置
二.参考文档 1.Log4j 与 logback对比.及使用配置
- java对象的初始化过程和创建对象的几种方式
1.加载父类,加载父类的静态属性和静态代码块 2.加载子类,加载子类的静态属性和静态代码块 3.初始化父类中的非静态属性并赋初值,执行父类非静态代码块,执行父类构造. 4.初始化子类中的非静态属性并赋 ...