gpt4 book ai didi

c# - 用于实现 "Search as you type"Combobox 的 BackgroundWorker

转载 作者:太空宇宙 更新时间:2023-11-03 16:27:47 26 4
gpt4 key购买 nike

我为我的组合框创建了一个代码,它可以在存储过程的帮助下在 Sql Server 上的一个非常大的表中搜索地址(我正在使用 Entity Framework )。我的存储过程返回 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;
}

有没有办法在没有 backgroundworker 的情况下加快速度?

最佳答案

创建一个按钮,你将调用 searchbutton并在此按钮的 click_event 中调用运行 backgroundworker 的 search() 方法填充组合框清除组合框的 key_press 事件,它将起作用错误是你的 key_press 事件调用了你的搜索方法发生的每一次击键所以取回它

关于c# - 用于实现 "Search as you type"Combobox 的 BackgroundWorker,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12010219/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com