在WinForm中开发的过程中,所有继承了Control控件,在使用多线程的时候,就需要通过一个通用的方法来设置界面上的控件的属性。

        delegate void DelegateSetControl(Control ctrObj, string attrName, object attrVal);

        public static void SetControlValue(Control ctrObj, string attrName, object attrVal)
        {
            if (ctrObj.InvokeRequired)
            {
                DelegateSetControl delegateSetButtion = new DelegateSetControl(SetControlValue);//实例化委托对象
                ctrObj.Invoke(delegateSetButtion, ctrObj, attrName, attrVal);
            }
            else
            {
                ctrObj.GetType().GetProperty(attrName).SetValue(ctrObj, attrVal);
            }
        }

 

所以,才有了下面的方法:

 

Control control = Controls.Find("button1", true)[0];

object o = control.GetType().GetProperty("PropertyName").GetValue(control, null);

System.Reflection.EventInfo ev = control.GetType().GetEvent("Click");

你这是要获取事件吧,第二行是获取属性,第三行是获取事件

 

出处:https://blog.csdn.net/lunyesheng/article/details/52932477

======================================================

顺便看到网上有 WebForm 获取控件的,也一起贴出来吧!

    t.Attributes.Add("s""abc");
    Response.Write(t.Attributes["s"]);

 

出处:https://bbs.csdn.net/topics/310129526

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-06-22
  • 2022-12-23
  • 2022-03-08
  • 2021-11-23
猜你喜欢
  • 2021-12-28
  • 2021-07-14
  • 2021-12-22
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-01-31
相关资源
相似解决方案