c# 控件文本通用代理委托设置,解决不同线程间设置控件问题

来源:互联网 发布:手机相册查看软件 编辑:程序博客网 时间:2024/04/29 04:12

解决不同线程控件不可操作的问题,通用性和扩展性比较强,一般控件都可以使用 

 

        delegate void SetControlTextCallback(Control control, string text, string type);           /// <summary>        /// 委托处理空间显示的文本          /// </summary>        /// <param name="control">控件</param>        /// <param name="text">文本</param>        /// <param name="type">类型 TextBox StatusStrip</param>        public void SetControlText(Control control,string text,string type)        {            try            {                if (control.InvokeRequired)                {                    SetControlTextCallback d = new SetControlTextCallback(SetControlText);                    this.Invoke(d, new object[] { control,text, type });                }                else                {                    switch(type)                    {                        case "TextBox" :                            {                                ((TextBox)control).Text = text;                                break;                            }                        case "StatusStrip":                            {                                ((StatusStrip)control).Text = text;                                break;                            }                        case "Label":                            {                                ((Label)control).Text = text;                                break;                            }                    }                }            }            catch            {            }        }

原创粉丝点击