Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# “实施”的背景工作人员;键入时搜索";组合框_C#_Search_Frameworks_Combobox_Entity - Fatal编程技术网

C# “实施”的背景工作人员;键入时搜索";组合框

C# “实施”的背景工作人员;键入时搜索";组合框,c#,search,frameworks,combobox,entity,C#,Search,Frameworks,Combobox,Entity,我已经为我的组合框创建了一个代码,它可以在Sql Server上借助存储过程(我正在使用Entity framework)在一个非常大的表中搜索地址。我的存储过程返回10次点击,我的代码用搜索结果填充组合框。为此,我使用BackgroundWorker 但我现在遇到了大问题: -尽管组合框中充满了我的搜索结果,但它始终选择了第一个项目。即使我只键入一个字母,整个文本也会被选中 此后,搜索地址不再有效。它只在这10个结果中搜索,我不知道如何解决这个问题。这是我的全部代码,它给我带来了问题: pu

我已经为我的组合框创建了一个代码,它可以在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 });
        }  
    }
    
    公共字符串searchedItem=”“;
    公共委托void delegateUpdateComboxSelection(组合框myCombo、字符串值、整数计数);
    BackgroundWorker m_bgworker=新的BackgroundWorker();
    静态自动重置事件resetWorker=新自动重置事件(false);
    m_bgworker.WorkerSupportsScanCellation=true;
    m_bgworker.DoWork+=新的DoWorkEventHandler(FillComboboxBindingList);
    m_bgworker.RunWorkerCompleted+=新的RunWorkerCompletedEventHandler(m_bgworker\u RunWorkerCompleted);
    BindingList m_地址=新BindingList();
    void m_bgworker_RunWorkerCompleted(对象发送方,RunWorkerCompletedEventArgs e)
    {
    int count=(int)((object[])e.Result)[0];
    字符串值=(字符串)((对象[])e.Result)[1];
    组合框myCombo=(组合框)((对象[])e.Result)[2];
    DelegateUpdateComboxSelection ndelegate=新的DelegateUpdateComboxSelection(UpdateComboxSelection);
    if(this.invokererequired)
    {
    调用(ndelegate,新对象[]{myCombo,value,count});
    返回;
    }
    其他的
    {
    更新mboselection(myCombo、value、count);
    返回;
    }               
    }
    私有void UpdateComboSelection(组合框myCombo、字符串值、整数计数)
    {
    myCombo=combox9;
    myCombo.DataSource=m_地址;
    searchedItem=myCombo.Text;
    如果(计数>0)
    {
    myCombo.SelectionStart=value.Length;
    myCombo.SelectionLength=searchedItem.Length-value.Length;
    myCombo.DroppedDown=true;
    }
    其他的
    {
    myCombo.DroppedDown=false;
    myCombo.SelectionStart=value.Length;
    }
    } 
    public void FillComboboxBindingList(对象发送方,DoWorkEventArgs e)
    {
    如果(m_bgworker.CancellationPending)
    {
    resetWorker.Set();
    e、 取消=真;
    返回;
    }
    其他的
    {
    字符串值=(字符串)((对象[])e.Argument)[0];
    列出结果;
    结果=_vsebina.SP_searcher(value.ToList();
    m_地址=新绑定列表();
    foreach(SP_结果1 rez in结果)
    {
    if(m_地址包含(rez))
    {
    继续;
    }
    其他的
    {
    m_地址。添加(rez);
    }
    }
    foreach(m_addresses.ToArray()中的SP_Result1 r)
    {
    如果(!result.Contains(r))
    {
    m_地址。删除(r);
    }
    }
    e、 结果=新对象[]{rezultat.Count,vrednost,null};
    返回;
    }
    }
    私有void组合框9u KeyUp(对象发送方,KeyEventArgs e)
    {
    if(e.KeyCode==Keys.Back)
    {
    int searchStart=comboBox9.SelectionStart;
    如果(搜索开始>0)
    {
    搜索开始--;
    如果(searchStart==0)
    {
    comboBox9.Text=“”;
    }
    其他的
    {
    comboBox9.Text=comboBox9.Text.Substring(0,searchStart+1);
    }
    }
    其他的
    {
    searchStart=0;
    }
    e、 已处理=正确;
    }
    }
    私有void组合框9_Enter(对象发送方,事件参数e)
    {
    comboBox9.SelectionStart=0;
    comboBox9.SelectionLength=0;
    }
    私有无效组合框9_单击(对象发送方,事件参数e)
    {
    comboBox9.Text=“”;
    }
    私有无效组合框9u按键(对象发送器,按键事件参数e)
    {
    搜索();
    }
    公开无效搜索()
    {
    如果(comboBox9.Text.Length<4)
    {
    返回;
    }
    其他的
    {
    if(m_bgworker.IsBusy)
    {
    m_bgworker.CancelAsync();
    m_bgworker=新的BackgroundWorker();
    m_bgworker.WorkerSupportsScanCellation=true;
    m_bgworker.DoWork+=新的DoWorkEventHandler(FillComboboxBindingList);
    m_bgworker.RunWorkerCompleted+=新的RunWorkerCompletedEventHandler(m_bgworker\u RunWorkerCompleted);
    }
    m_bgworker.RunWorkerAsync(新对象[]{comboBox9.Text,comboBox9});
    }  
    }
    
也许有人能告诉我,我做错了什么。这是我第一次使用BackgroundWorker。我不知道怎么做 以任何其他方式使用combobox实现“键入时搜索”,因为我的带有地址的数据表相当大(百万条记录)

提前感谢您提供的任何帮助或代码示例

弗拉基米尔

编辑1: 好的,这是我的代码,在我使用BackGroundWorker之前。它工作正常,但搜索速度非常慢(可能需要10秒):

private void comboBox9\u text已更改(对象发送方,事件参数e)
{
如果(comboBox9.Text.Length<4)
{
返回;
}
其他的
{
FillCombobox(comboBox9.Text,comboBox9);
}
}
公共void FillCombobox(字符串值,ComboBox myCombo)
{
列出结果;
结果=_vsebina.spIskalnikNaslovi1(值).ToList();
如果(result.Count()>0)
{
myCombo.DataSource=结果;
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;
    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;
    }
    private bool _canUpdate = true;
    private bool _needUpdate = false;
    List<spIskalnikNaslovi_Result1> dataFound;

    private void comboBox12_TextChanged(object sender, EventArgs e)
    {
        if (_needUpdate)
        {
            if (_canUpdate)
            {
                _canUpdate = false;
                refreshData();
            }
            else
            {
                restartTimer();
            }
        }
    }

    private void comboBox12_KeyUp(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Back)
        {
            int searchStart = comboBox12.SelectionStart;
            if (searchStart > 0)
            {
                searchStart--;
                if (searchStart == 0)
                {
                    comboBox12.Text = "";
                }
                else
                {
                    comboBox12.Text = comboBox12.Text.Substring(0, searchStart + 1);
                }
            }
            else 
            {
                searchStart = 0;
            }
            e.Handled = true;
        }
    }

    private void comboBox12_TextUpdate(object sender, EventArgs e)
    {
        _needUpdate = true;
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        _canUpdate = true;
        timer1.Stop();
        refreshData();
    }

    private void refreshData()
    {
        if (comboBox12.Text.Length > 1)
        {
            FillCombobox(comboBox12.Text, comboBox12);
        }
    }

    private void restartTimer()
    {
        timer1.Stop();
        _canUpdate = false;
        timer1.Start();
    }


    private void FillCombobox(string value, ComboBox myCombo)
    {
        dataFound = _vsebina.spIskalnikNaslovi1(value).ToList();
        if (dataFound.Count() > 0)
        {
            myCombo.DataSource = dataFound;
            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;
            return;
        }
        else
        {
            myCombo.DroppedDown = false;
            myCombo.SelectionStart = value.Length;
            return;
        }            
    }