您的位置:首页 > 编程语言 > C#

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

2013-01-30 15:53 316 查看
解决不同线程控件不可操作的问题,通用性和扩展性比较强,一般控件都可以使用

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
{

}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐