• 反射设置当前窗体所有控件的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);
                                }
                            }
                        }
    具体设置代码
  • 相关阅读:
    protobuf 协议 windows 下 C++ 环境搭建
    protobuf 协议 windows 下 java 环境搭建
    ProtocolBuffers (二) android与PC,C#与Java 利用protobuf 进行无障碍通讯【Socket】
    C++ ofstream和ifstream详细用法
    C# 关于out和ref的问题
    java 解析office文件 大全
    Ezhuang
    IOS
    Android Client and PHP Server
    一个IM开源项目LiteTalk
  • 原文地址:https://www.cnblogs.com/sczmzx/p/3605390.html
Copyright © 2020-2023  润新知