zoukankan      html  css  js  c++  java
  • 反射获取窗体所有控件的Text

    可以直接通过反射获取当前窗体的所有控件的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
                    { }
                }
    代码
  • 相关阅读:

    python 爬取可用
    安装完出现Deprecated: Function ereg_replace() is deprecated in
    mysql数据库还原出错ERROR:Unknown command ‘\’解决手记
    mysql 常用语句
    This version of MySQL doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery 解决方法
    js 中 json对象 与 json字符串 间相互转换
    神器 Sublime Text 3 的一些常用快捷键
    神器 Sublime Text 3 的一些常用插件
    apache php gzip压缩输出的实现方法
  • 原文地址:https://www.cnblogs.com/sczmzx/p/3605349.html
Copyright © 2011-2022 走看看