作者wvsrugby (wvsrugby)
看板C_Sharp
标题Re: [问题] Text方法会丢出未知的例外?
时间Fri May 22 00:21:31 2009
提供两个肮脏的 Method, 针对跨执行绪变更 UI 的问题.
//设定控制项的 Text 属性
Ctrl.SetText(textBox1, "TextValue"); //将 textBox1.Text 设为 "TextValue"
//设定控制项的任一属性值
Ctrl.SetValue(textBox1, "Visible", false); //将 textBox1 设为不可见
//CtrlHelper
public static class Ctrl
{
public static void SetValue(
Control ctrl,
string property,
object value)
{
if (ctrl.InvokeRequired)
{
Action<Control, string, object> d =
new Action<Control, string, object>(SetValue);
ctrl.Invoke(d, ctrl, property, value);
return;
}
ctrl.GetType().GetProperty(property)
.SetValue(ctrl, value, null);
}
public static void SetText(
Control ctrl,
string value)
{
SetValue(ctrl, "Text", value);
}
}
--
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 218.161.40.29