反射设置当前窗体所有控件的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; } 遍历某个 ...
随机推荐
- Cocos2d-x 3.0final 终结者系列教程16-《微信飞机大战》实现
看到cocos2d-x推出了3.1版本号,真是每月一次新版本号,速度. 另一个好消息就是http://cn.cocos2d-x.org/上线了,祝贺!啥时候把我的视频和教程放上去呢?!! . 视频下载 ...
- Python 单元测试 之setUP() 和 tearDown()
setUp:表示前置条件,它在每一个用例执行之前必须会执行一次 setUp可以理解为我们需要自动化测试时,需要打开网页窗口,输入对应测试地址,这一些属于前置条件. tearDown:表示释放资源,它在 ...
- delphi Base64编码/解码及数据压缩/解压知识
一.Base64编码/解码 一般用到的是Delphi自带的单元EncdDecd,当然还有第三方提供的单元或控件,其中我所接触到的认为比较好的有Indy的TIdMimeEncode / TIdMimeD ...
- sort()函数到底是怎样进行数字排序的
很多人会用sort(),并不见得知道它具体是怎样给数字排序的.其实不知道也行,会用就可以,感兴趣的可以来看看. var numberArray = [2,4,1,3]; numberArray.sor ...
- Linux就该这么学--Shell脚本条件语句(一)
1.条件测试语句能够让Shell脚本根据实际工作灵活调整工作内容,例如判断系统的状态后执行指定的工作,或创建指定数量的用户,批量修改用户密码,这些都可以让Shell脚本通过条件测试语句完成. if条件 ...
- Pentaho BIServer Community Edtion 6.1 使用教程 第三篇 发布和调度Kettle(Data Integration) 脚本 Job & Trans
Pentaho BIServer Community Edtion 6.1 集成了 Kettle 组件,可以运行Kettle 程序脚本.但由于Kettle没有直接发布到 BIServer-ce 服务的 ...
- Django—工程创建以及models数据库易错点
Python的WEB框架有Django.Tornado.Flask 等多种,Django相较与其他WEB框架其优势为:大而全,框架本身集成了ORM.模型绑定.模板引擎.缓存.Session等诸多功能. ...
- php远程下载文件
<?php /* 本源码来源于网络 http://user.qzone.qq.com/292672703 */ header("content-Type: text/html; cha ...
- iOS中常见的设计模式(MVC/单例/委托/观察者)
关于设计模式这个问题,在网上也找过一些资料,下面是我自己总结的,分享给大家 如果你刚接触设计模式,我们有好消息告诉你!首先,多亏了Cocoa的构建方式,你已经使用了许多的设计模式以及被鼓励的最佳实践. ...
- NodeJs如何全局统一处理异常,实现RestFull风格
当在controller中处理客户端发来的数据时,我们会去校验数据,当数据错误时,我们会给客户端返回一个信息,如: export function add (req, res, next) { con ...