我为我的组合框创建了一个代码,它可以在存储过程的帮助下在 Sql Server 上的一个非常大的表中搜索地址(我正在使用实体框架)。我的存储过程返回 10 次点击,我的代码用搜索结果填充组合框。为此,我正在使用 BackgroundWorker。
但是在这里我现在遇到了大问题: - 虽然组合框充满了我的搜索结果,但它总是选择第一个项目。即使我只输入一个字母,整个文本也会被选中;
之后搜索地址不再起作用。它只在这 10 个结果中搜索,我不知道如何解决这个问题。这是我的整个代码,这给我带来了问题:
public String searchedItem = ""; public delegate void DelegateUpdateComboboxSelection(ComboBox myCombo,string value,int count); BackgroundWorker m_bgworker = new BackgroundWorker(); static AutoResetEvent resetWorker = new AutoResetEvent(false); m_bgworker.WorkerSupportsCancellation = true; m_bgworker.DoWork += new DoWorkEventHandler(FillComboboxBindingList); m_bgworker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(m_bgworker_RunWorkerCompleted); BindingList<spIskalnikNaslovi_Result1> m_addresses = new BindingList<SP_Result1>(); void m_bgworker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { int count = (int)((object[])e.Result)[0]; string value = (string)((object[])e.Result)[1]; ComboBox myCombo = (ComboBox)((object[])e.Result)[2]; DelegateUpdateComboboxSelection ndelegate = new DelegateUpdateComboboxSelection(UpdateComboSelection); if (this.InvokeRequired) { Invoke(ndelegate, new object[] {myCombo, value, count}); return; } else { UpdateComboSelection(myCombo, value, count); return; } } private void UpdateComboSelection(ComboBox myCombo, String value, int count) { myCombo = comboBox9; myCombo.DataSource = m_addresses; searchedItem = myCombo.Text; if (count > 0) { myCombo.SelectionStart = value.Length; myCombo.SelectionLength = searchedItem.Length - value.Length; myCombo.DroppedDown = true; } else { myCombo.DroppedDown = false; myCombo.SelectionStart = value.Length; } } public void FillComboboxBindingList(object sender, DoWorkEventArgs e) { if (m_bgworker.CancellationPending) { resetWorker.Set(); e.Cancel = true; return; } else { string value = (String)((Object[])e.Argument)[0]; List<SP_Result1> result; result = _vsebina.SP_searcher(value).ToList<SP_Result1>(); m_addresses = new BindingList<SP_Result1>(); foreach (SP_Result1 rez in result) { if (m_addresses.Contains(rez)) { continue; } else { m_addresses.Add(rez); } } foreach (SP_Result1 r in m_addresses.ToArray()) { if (!result.Contains(r)) { m_addresses.Remove(r); } } e.Result = new object[] { rezultat.Count, vrednost, null }; return; } } private void comboBox9_KeyUp(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Back) { int searchStart = comboBox9.SelectionStart; if (searchStart > 0) { searchStart--; if (searchStart == 0) { comboBox9.Text = ""; } else { comboBox9.Text = comboBox9.Text.Substring(0, searchStart + 1); } } else { searchStart = 0; } e.Handled = true; } } private void comboBox9_Enter(object sender, EventArgs e) { comboBox9.SelectionStart = 0; comboBox9.SelectionLength = 0; } private void comboBox9_Click(object sender, EventArgs e) { comboBox9.Text = ""; } private void comboBox9_KeyPress(object sender, KeyPressEventArgs e) { Search(); } public void Search() { if (comboBox9.Text.Length < 4) { return; } else { if (m_bgworker.IsBusy) { m_bgworker.CancelAsync(); m_bgworker = new BackgroundWorker(); m_bgworker.WorkerSupportsCancellation = true; m_bgworker.DoWork += new DoWorkEventHandler(FillComboboxBindingList); m_bgworker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(m_bgworker_RunWorkerCompleted); } m_bgworker.RunWorkerAsync(new object[] { comboBox9.Text, comboBox9 }); } }
也许有人可以启发我,我做错了什么。这是我第一次使用 BackgroundWorker。我不知道如何以任何其他方式使用组合框实现“键入时搜索”,因为我的地址数据表非常大(百万条记录)。
在此先感谢您提供任何帮助或代码示例。
弗拉基米尔
编辑 1:好的,这是我的代码,在我使用 BackGroundWorker 之前。它有效,但搜索速度非常慢(最多可能需要 10 秒):
private void comboBox9_TextChanged(object sender, EventArgs e)
{
if (comboBox9.Text.Length < 4)
{
return;
}
else
{
FillCombobox(comboBox9.Text, comboBox9);
}
}
public void FillCombobox(string value, ComboBox myCombo)
{
List<spIskalnikNaslovi_Result1> result;
result = _vsebina.spIskalnikNaslovi1(value).ToList();
if (result.Count() > 0)
{
myCombo.DataSource = result;
myCombo.ValueMember = "HS_MID";
myCombo.DisplayMember = "NASLOV1";
var searchedItem = myCombo.Items[0].ToString();
myCombo.SelectionStart = value.Length;
myCombo.SelectionLength = searchedItem.Length - value.Length;
myCombo.DroppedDown = true;
}
else
{
myCombo.DroppedDown = false;
myCombo.SelectionStart = value.Length;
}
return;
}
有没有办法在没有后台工作人员的情况下加快速度?