所谓的潇洒

导航

下拉框分步加载

对话框加载时,填充10条选项

list= getData().Take(10).ToList();//从数据库查询前10条数据
list.Add(new Student { ID = 0, Name = "加载更多..." });
comboBox1.Items.AddRange(list);
comboBox1.ValueMember = "ID";
comboBox1.DisplayMember = "Name";

重写事件

        private void comboBox1_TextUpdate(object sender, EventArgs e)
        {
            if (!comboBox1.Focused) return;
            try
            {
                string text = comboBox1.Text;
                comboBox1.Items.Clear();
                comboBox1.Items.AddRange(list.Where(x => x.Name.Contains(text)).ToList());
                //自动弹出下拉框                    
                comboBox1.DroppedDown = true;
                comboBox1.Text = text;
                comboBox1.SelectionStart = comboBox1.Text.Length;//设置光标位置,否则光标位置始终保持在第一列,造成输入关键词的倒序排列                   
                Cursor = Cursors.Default;//保持鼠标指针原来状态,有时候鼠标指针会被下拉框覆盖,所以要进行一次设置。
            }
            catch (Exception) { }
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                int Id = ((Student)comboBox1.SelectedItem).ID;
                if (Id==0)
                {
                    comboBox1.Items.RemoveAt(comboBox1.Items.Count - 1);
                    var items= list.Skip(comboBox1.Items.Count).Take(10);                    
                    comboBox1.Items.AddRange(items);
                    if (comboBox1.Items.Count < list.Count) comboBox1.Items.Add(new Student { ID = 0, Name = "加载更多..." });
                    return;
                }
            }
            catch (Exception) { }
        }

 

posted on 2020-12-25 16:54  所谓的潇洒  阅读(135)  评论(0编辑  收藏  举报