在C#开发中,不可避免的会用到多线程,在Winform程序中如果直接设置控件会报错,本文内容解决了多线程设置控件的问题,举了三种控件的例子,包括Textbox Checkbox Button等控件。如果涉及其他控件可以参考本文例子,以此类推开发。
#region 控件操作
private void SetCheckBox(CheckBox cbxctrl, bool b)
{
if (cbxctrl.InvokeRequired)
{
cbxctrl.Invoke((EventHandler)(delegate
{
((CheckBox)cbxctrl).Checked = b;
}));
}
else
{
((CheckBox)cbxctrl).Checked = b;
}
}
private void SetTextBox(TextBox txtctrl, string strvalue)
{
if (txtctrl.InvokeRequired)
{
txtctrl.Invoke((EventHandler)(delegate
{