栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

C#Winforms组合框动态自动完成

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C#Winforms组合框动态自动完成

这是我的最终解决方案。它可以处理大量数据。我

Timer
用来确保用户要查找当前值。看起来很复杂,但事实并非如此。感谢 Max Lambertini
的想法。

        private bool _canUpdate = true;        private bool _needUpdate = false;        //If text has been changed then start timer        //If the user doesn't change text while the timer runs then start search        private void combobox1_TextChanged(object sender, EventArgs e)        { if (_needUpdate) {     if (_canUpdate)     {         _canUpdate = false;         UpdateData();  }     else     {         RestartTimer();     } }        }        private void UpdateData()        { if (combobox1.Text.Length > 1) {     List<string> searchData = Search.GetData(combobox1.Text);     HandleTextChanged(searchData); }        }        //If an item was selected don't start new search        private void combobox1_SelectedIndexChanged(object sender, EventArgs e)        { _needUpdate = false;        }        //Update data only when the user (not program) change something        private void combobox1_TextUpdate(object sender, EventArgs e)        { _needUpdate = true;        }        //While timer is running don't start search        //timer1.Interval = 1500;        private void RestartTimer()        { timer1.Stop(); _canUpdate = false; timer1.Start();        }        //Update data when timer stops        private void timer1_Tick(object sender, EventArgs e)        { _canUpdate = true; timer1.Stop(); UpdateData();        }        //Update combobox with new data        private void HandleTextChanged(List<string> dataSource)        { var text = combobox1.Text; if (dataSource.Count() > 0) {     combobox1.DataSource = dataSource;     var sText = combobox1.Items[0].ToString();     combobox1.SelectionStart = text.Length;     combobox1.SelectionLength = sText.Length - text.Length;     combobox1.DroppedDown = true;     return; } else {     combobox1.DroppedDown = false;     combobox1.SelectionStart = text.Length; }        }

这个解决方案不是很酷。因此,如果有人有其他解决方案,请与我分享。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/421506.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号