反射获取窗体所有控件的Text
2024-08-30 02:14:32
可以直接通过反射获取当前窗体的所有控件的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
{ }
}
代码
最新文章
- [No000087]Linq排序,SortedList排序,二分法排序性能比较
- 判断是否IPv6网络
- 【转】Python资源 转自 dylanninin.com
- VB 笔记
- Dependency Injection in ASP.NET Core
- D. Green and Black Tea
- flex 简单跑马灯效果(竖着显示)
- python基础===zip在python3中的用法
- MAC下用homebrew安装及配置apache、php和mysql
- js 数据加载loading封装
- STM32学习笔记
- [C]gcc编译器的一些常用语法
- ASP.NET Web API实现微信公众平台开发(二)access_token与定时任务
- apache httpd.conf
- 微信小程序制作家庭记账本之六
- JavaScript Math和Number对象研究
- python3爬虫-爬取新浪新闻首页所有新闻标题
- SqlServer收缩日志文件
- Selenium2+python自动化51-unittest简介
- 2016";百度之星"; - 初赛(Astar Round2B)1003 瞬间移动 组合数学+逆元