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

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

 

        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
            {

            }
        }

你可能感兴趣的:(c# 控件文本通用代理委托设置,解决不同线程间设置控件问题)