在Winfrom下实现类似百度、Google搜索自能提示功能
前记:数据源来自页面的一个ComboBox的数据源List<Contract>集合
页面放置一个TextBox(搜索框)、ListBox(显示搜索出来的数据),ListBox位置位于TextBox正下方,初始化隐藏。
TextBox--->txtSearch ListBox--->listBox1关于两个控件的事件,详见代码:
- #region 自能提示
- Point pList = new Point();
- private void txtSearch_TextChanged(object sender, EventArgs e)
- {
- if (this.txtSearch.Text.Trim() != "")
- {
- List<Contract> source = getDataTable(this.txtSearch.Text.Trim());
- BindList(source);
- this.listBox1.Visible = true;
- }
- else
- {
- this.listBox1.Items.Clear();
- this.listBox1.Visible = false;
- }
- }
- private void txtSearch_KeyDown(object sender, KeyEventArgs e)
- {
- if (e.KeyCode == Keys.Down && this.listBox1.Visible)
- {
- this.listBox1.Focus();
- if (this.listBox1.SelectedItems.Count > 0)
- {
- this.listBox1.SetSelected(this.listBox1.SelectedIndex, false);
- }
- if (this.listBox1.Items.Count > 0)
- {
- this.listBox1.SetSelected(0, true);
- }
- }
- }
- private void listBox1_MouseUp(object sender, MouseEventArgs e)
- {
- if (this.listBox1.SelectedItems.Count > 0)
- {
- this.txtSearch.Text = this.listBox1.Text;
- cboChoCont.Text = this.txtSearch.Text;
- this.listBox1.Visible = false;
- this.txtSearch.Focus();
- }
- }
- private void listBox1_MouseMove(object sender, MouseEventArgs e)
- {
- Point m = new Point(e.X, e.Y);
- int index = GetItemAt(this.listBox1, e.X, e.Y);
- if (this.listBox1.SelectedItems.Count > 0 && this.listBox1.SelectedIndex != index)
- {
- this.listBox1.SetSelected(this.listBox1.SelectedIndex, false);
- }
- if (index != -1 && this.listBox1.SelectedIndex != index)
- {
- this.listBox1.SetSelected(index, true);
- }
- }
- private void listBox1_KeyDown(object sender, KeyEventArgs e)
- {
- if (e.KeyCode == Keys.Enter && this.listBox1.Visible && this.listBox1.SelectedItems.Count > 0)
- {
- this.txtSearch.Text = this.listBox1.SelectedItems[0].ToString();
- this.cboChoCont.Text = this.txtSearch.Text;
- this.listBox1.Visible = false;
- this.txtSearch.Focus();
- }
- }
- private bool GetItemAt(Point Mousep, int index)
- {
- int ph = this.listBox1.GetItemHeight(index) * index;
- int ph1 = this.listBox1.GetItemHeight(index) * index + this.listBox1.GetItemHeight(index);
- if (Mousep.Y > ph && Mousep.Y < ph1 && Mousep.X > 0 && Mousep.X < this.listBox1.Width)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- private int GetItemAt(ListBox listBox, int X, int Y)
- {
- int index = -1;
- for (int i = 0; i < listBox.Items.Count; i++)
- {
- System.Drawing.Rectangle r = listBox.GetItemRectangle(i);
- if (r.Contains(new Point(X, Y)))
- {
- index = i; ;
- break;
- }
- }
- return index;
- }
- private void BindList(List<Contract> source)
- {
- this.listBox1.Items.Clear();
- for (int i = 0; i < source.Count; i++)
- {
- this.listBox1.Items.Add(source[i].Con_Name_Serial);
- }
- if (source.Count < 11)
- {
- this.listBox1.Height = 15 * source.Count + 15;
- }
- else
- {
- this.listBox1.Height = 150;
- }
- this.listBox1.Visible = true;
- }
- private List<Contract> getDataTable(string s)
- {
- List<Contract> searchList = new List<Contract>();
- int length = list.Count;
- for (int i = 0; i < length; i++)
- {
- if (list[i].Con_Name_Serial.IndexOf(s) == 0)
- searchList.Add(list[i]);
- }
- return searchList;
- }
- private void listBox1_DrawItem(object sender, DrawItemEventArgs e)
- {
- // Set the DrawMode property to draw fixed sized items.
- listBox1.DrawMode = DrawMode.OwnerDrawFixed;
- // Draw the background of the ListBox control for each item.
- e.DrawBackground();
- // Define the default color of the brush as black.
- Brush myBrush = Brushes.Black;
- FontFamily fontFamily = new FontFamily("宋体");
- System.Drawing.Font myFont = new Font(fontFamily, 9);
- // Determine the color of the brush to draw each item based on the index of the item to draw.
- if((e.State & DrawItemState.Selected) == DrawItemState.Selected)
- {
- //e.Graphics.FillRectangle(Brushes.Blue, e.Bounds);
- if(e.Index > -1)
- {
- e.Graphics.DrawString(listBox1.Items[e.Index].ToString(), myFont, Brushes.White, e.Bounds, StringFormat.GenericDefault);
- }
- }
- else
- {
- //e.Graphics.FillRectangle(Brushes.White, e.Bounds);
- if(e.Index > -1)
- {
- e.Graphics.DrawString(listBox1.Items[e.Index].ToString(), myFont, myBrush, e.Bounds, StringFormat.GenericDefault);
- }
- }
- // Draw the current item text based on the current Font and the custom brush settings.
- //e.Graphics.DrawString(listBox1.Items[e.Index].ToString(), e.Font, myBrush, e.Bounds, StringFormat.GenericDefault);
- // If the ListBox has focus, draw a focus rectangle around the selected item.
- e.DrawFocusRectangle();
- }
- #endregion
转载来自:http://blog.csdn.net/Rock870210/article/details/5856235
在Winfrom下实现类似百度、Google搜索自能提示功能的更多相关文章
- 【搜索引擎】 PostgreSQL 10 实时全文检索和分词、相似搜索、模糊匹配实现类似Google搜索自动提示
需求分析 要通过PostgreSQL实现类似Google搜索自动提示的功能,例如要实现一个查询海量数据中的商品名字,每次输入就提示用户各种相关搜索选项,例如淘宝.京东等电商查询 思路 这个功能可以用 ...
- Jquery实现类似百度的搜索框
最近工作中需要做一个搜索框,类似百度的搜索框,需要达到两个功能: 1.输入关键字,展示匹配的下拉列表 2.选择匹配的项后查出相关内容 一般电商网站中也经常用到该搜索条,首先分析功能实现,输入关键字马上 ...
- ajax+JQuery实现类似百度智能搜索框
最近再学习ajax,上课老师让我们实现一个类似百度首页实现搜索框的功能,刚开始做的时候没有一点头绪,查阅大量网上的资源后,发现之前的与我们现在的有些区别,所以在此写出来,希望能对大家有所帮助. 下面先 ...
- C#winform抓取百度,Google搜索关键词结果
基于网站seo,做了一采集百度和Google搜索关键字结果的采集.在这里与大家分享一下 先看先效果图 代码附加: 1 private void baidu_Click(object sender ...
- Jquery实现搜索框提示功能
博客的前某一篇文章中http://www.cnblogs.com/hEnius/p/2013-07-01.html写过一个用Ajax来实现一个文本框输入的提示功能.最近在一个管理项目的项目中,使用后发 ...
- **IOS自动完成(搜索自动提示)功能实现
UISearchBar搜索AutoComplete下拉列表搜索提示 http://www.codeios.com/thread-10685-1-1.html 介绍: 在搜索框上加入下拉列表.在 ...
- AJAX实现类似百度的搜索提示,自动补全和键盘、鼠标操作
<script type="text/javascript"> $(document).ready(function(){ var highlightIndex = - ...
- 使用jqueryUI和corethink实现的类似百度的搜索提示
代码:http://download.csdn.net/detail/u012995856/9676845 效果: 目录: 这里是以corethink模块的形式,只需要安装上访问 index.php? ...
- vue单页面条件下添加类似浏览器的标签页切换功能
在用vue开发的时候,单页面应用程序,而又有标签页这种需求,各种方式实现不了, 从这个 到这个,然后再返回上面那个 因为每个标签页的route不一样,导致组件重新渲染的问题,怎么都不知道如何实现... ...
随机推荐
- Android提高第十二篇之蓝牙传感应用
请问淘宝上买的单片机蓝牙模块与安卓/android手机通讯的时候需要设置UUID吗? 2013-02-15 09:39 在世张辽 | 浏览 2769 次 想用安卓手机和单片机通过蓝牙模块通讯, ...
- Monkey and Banana(基础DP)
Monkey and Banana Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
- Android Studio的使用(三)--包不分级、修改包名
1.如果不喜欢将包逐级展开的话,可以将每一个包名都完整展现出来,只需要勾选Flatten Packages. 2.修改包名 3.填写新的包名 4.点击确认
- CodeForces 614C Peter and Snow Blower
简单计算几何,只要算出圆心到多边形上的最短距离和最长距离即可 #include<cstdio> #include<cstring> #include<cmath> ...
- PAT 天梯赛 L2-005 集合相似度
set的应用 题目链接 题解 有点像集合的交并操作,直接利用set进行处理,因为set有去重的功能,而且set是利用红黑树实现的,查找速度快O(logN). 代码如下: #include<cst ...
- @media max-width 与jquery宽度取值的差异
最近写了个响应式网站,有些效果通过用jq的$(window).width()来判断屏幕宽度,当屏幕宽度小于1366时一些参数发生变化.@media中也有小于1366的判断条件,但是用起来的时候发现一个 ...
- JS——无缝滚动
1.描述——无缝滚动图片 2.代码 <!DOCTYPE html> <html> <head> <meta charset="UTF-8" ...
- ARC下需要注意的内存管理
ARC下需要注意的内存管理 2016/04/03 · iOS开发 · 内存管理 分享到:1 原文出处: 一不(@luoyibu) 之前发了一篇关于图片加载优化的文章,还是引起很多人关注的,不过也 ...
- php中__clone() shallow copy 只是浅复制
什么是浅复制呢? 简单一点,就是说复制一个对象的时候,如果对象$Obj的一个属性的类型是引用类型的,比如 $person这个属性,指向的是一个 叫做 $objPerson的一个引用, 那么复制$Obj ...
- Unity3d 开发之 ulua 坑的总结
相同的 lua 代码在安卓上能正常运行,但在 IOS 上可能不会正常运行而导致报红,崩溃等,我在使用 lua 编程时遇到的一些坑总结如下: 1. File.ReadAllText, 诸如以下代码在 i ...