利用委托跨线程访问窗体中的控件,怎么TextBox可以访问,ComboBox却不能访问?
//声明
private delegate string mydelMember();
private string GetSearchWhere()
{
string result = string.Empty;
if (myTextBoxMemberID.Text.Trim() != "")
{
result = " and memberid='" + myTextBoxMemberID.Text.Trim() + "'";
}
if (myTextBoxMemberName.Text.Trim() != "")
{
result += " and membername like '%" + myTextBoxMemberName.Text.Trim() + "%'";
}
if (myTextBoxIDCard.Text.Trim() != "")
{
result += " and idcard = '" + myTextBoxIDCard.Text.Trim() + "'";
}
if (myTextBoxGZDW.Text.Trim() != "")
{
result += " and gzdw like '%" + myTextBoxGZDW.Text.Trim() + "%'";
}
if (myTextBoxjfdy .Text.Trim() != "")
{
result += " and xsjf >= " + myTextBoxjfdy.Text.Trim() + "";
}
if (myTextBoxjfxy .Text.Trim() != "")
{
result += " and xsjf <= " + myTextBoxjfxy.Text.Trim() + "";
}
if (comBoxPricType.IsHandleCreated)
{
if (comBoxPricType.Items.Count > 0)
{
if (comBoxPricType.SelectedIndex >= 0)
{
result += " and prictypeid = '" + comBoxPricType.Items[comBoxPricType.SelectedIndex].ToString().Substring(0, comBoxPricType.Items[comBoxPricType.SelectedIndex].ToString().IndexOf(".")) + "'";
}
}
}
if (myTextBoxZKL.Text.Trim()!="" )
{
result += " and zkrate <" + myTextBoxZKL.Text.Trim() + "";
}
return result;
}
调用:
using (BackgroundWorker backgroundWorker = new BackgroundWorker())
{
// 异步获取数据
backgroundWorker.DoWork += new DoWorkEventHandler(delegate(object o, DoWorkEventArgs workerEventArgs)
{
//this.BeginInvoke(new mydelMember(this.GetSearchWhere));
this.BeginInvoke(new myDelegatDoProcess(doProcess));
mydelMember strSearChWhere = new mydelMember(GetSearchWhere);//调用
string strTemp = strSearChWhere();
fCommandText = "select * from emberinfo " + strTemp;
//string a = this.BeginInvoke(new mydel(GetSearchWhere()));
using (SqlDataAdapter fAdapter = new SqlDataAdapter(fCommandText, fConnection))
{
try
{
fIsCommandIsCancelOrError = false;
fCurCommand = fAdapter.SelectCommand;
fCurCommand.CommandTimeout = 0;// 永不超时
fdsResult = new DataSet();
fAdapter.Fill(fdsResult);
fCurCommand = null;
}
catch (Exception x)
{
fIsCommandIsCancelOrError = true; //查询出错或用户已取消查询
}
}
});
// 数据获取完成,绑定数据
backgroundWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(delegate(object o, RunWorkerCompletedEventArgs x)
{
if (!fIsCommandIsCancelOrError)
{
dtResult = fdsResult.Tables[0];
}
if (fIsCommandIsCancelOrError == false)
{
this.tsPressBarQry.Value = 100;
}
this.DialogResult = DialogResult.OK;
});
backgroundWorker.RunWorkerAsync();
}