前记:数据源来自页面的一个ComboBox的数据源List<Contract>集合

页面放置一个TextBox(搜索框)、ListBox(显示搜索出来的数据),ListBox位置位于TextBox正下方,初始化隐藏。

TextBox--->txtSearch  ListBox--->listBox1关于两个控件的事件,详见代码:

  1. #region  自能提示
  2. Point pList = new Point();
  3. private void txtSearch_TextChanged(object sender, EventArgs e)
  4. {
  5. if (this.txtSearch.Text.Trim() != "")
  6. {
  7. List<Contract> source = getDataTable(this.txtSearch.Text.Trim());
  8. BindList(source);
  9. this.listBox1.Visible = true;
  10. }
  11. else
  12. {
  13. this.listBox1.Items.Clear();
  14. this.listBox1.Visible = false;
  15. }
  16. }
  17. private void txtSearch_KeyDown(object sender, KeyEventArgs e)
  18. {
  19. if (e.KeyCode == Keys.Down && this.listBox1.Visible)
  20. {
  21. this.listBox1.Focus();
  22. if (this.listBox1.SelectedItems.Count > 0)
  23. {
  24. this.listBox1.SetSelected(this.listBox1.SelectedIndex, false);
  25. }
  26. if (this.listBox1.Items.Count > 0)
  27. {
  28. this.listBox1.SetSelected(0, true);
  29. }
  30. }
  31. }
  32. private void listBox1_MouseUp(object sender, MouseEventArgs e)
  33. {
  34. if (this.listBox1.SelectedItems.Count > 0)
  35. {
  36. this.txtSearch.Text = this.listBox1.Text;
  37. cboChoCont.Text = this.txtSearch.Text;
  38. this.listBox1.Visible = false;
  39. this.txtSearch.Focus();
  40. }
  41. }
  42. private void listBox1_MouseMove(object sender, MouseEventArgs e)
  43. {
  44. Point m = new Point(e.X, e.Y);
  45. int index = GetItemAt(this.listBox1, e.X, e.Y);
  46. if (this.listBox1.SelectedItems.Count > 0 && this.listBox1.SelectedIndex != index)
  47. {
  48. this.listBox1.SetSelected(this.listBox1.SelectedIndex, false);
  49. }
  50. if (index != -1 && this.listBox1.SelectedIndex != index)
  51. {
  52. this.listBox1.SetSelected(index, true);
  53. }
  54. }
  55. private void listBox1_KeyDown(object sender, KeyEventArgs e)
  56. {
  57. if (e.KeyCode == Keys.Enter && this.listBox1.Visible && this.listBox1.SelectedItems.Count > 0)
  58. {
  59. this.txtSearch.Text = this.listBox1.SelectedItems[0].ToString();
  60. this.cboChoCont.Text = this.txtSearch.Text;
  61. this.listBox1.Visible = false;
  62. this.txtSearch.Focus();
  63. }
  64. }
  65. private bool GetItemAt(Point Mousep, int index)
  66. {
  67. int ph = this.listBox1.GetItemHeight(index) * index;
  68. int ph1 = this.listBox1.GetItemHeight(index) * index + this.listBox1.GetItemHeight(index);
  69. if (Mousep.Y > ph && Mousep.Y < ph1 && Mousep.X > 0 && Mousep.X < this.listBox1.Width)
  70. {
  71. return true;
  72. }
  73. else
  74. {
  75. return false;
  76. }
  77. }
  78. private int GetItemAt(ListBox listBox, int X, int Y)
  79. {
  80. int index = -1;
  81. for (int i = 0; i < listBox.Items.Count; i++)
  82. {
  83. System.Drawing.Rectangle r = listBox.GetItemRectangle(i);
  84. if (r.Contains(new Point(X, Y)))
  85. {
  86. index = i; ;
  87. break;
  88. }
  89. }
  90. return index;
  91. }
  92. private void BindList(List<Contract> source)
  93. {
  94. this.listBox1.Items.Clear();
  95. for (int i = 0; i < source.Count; i++)
  96. {
  97. this.listBox1.Items.Add(source[i].Con_Name_Serial);
  98. }
  99. if (source.Count < 11)
  100. {
  101. this.listBox1.Height = 15 * source.Count + 15;
  102. }
  103. else
  104. {
  105. this.listBox1.Height = 150;
  106. }
  107. this.listBox1.Visible = true;
  108. }
  109. private List<Contract> getDataTable(string s)
  110. {
  111. List<Contract> searchList = new List<Contract>();
  112. int length = list.Count;
  113. for (int i = 0; i < length; i++)
  114. {
  115. if (list[i].Con_Name_Serial.IndexOf(s) == 0)
  116. searchList.Add(list[i]);
  117. }
  118. return searchList;
  119. }
  120. private void listBox1_DrawItem(object sender, DrawItemEventArgs e)
  121. {
  122. // Set the DrawMode property to draw fixed sized items.
  123. listBox1.DrawMode = DrawMode.OwnerDrawFixed;
  124. // Draw the background of the ListBox control for each item.
  125. e.DrawBackground();
  126. // Define the default color of the brush as black.
  127. Brush myBrush = Brushes.Black;
  128. FontFamily fontFamily = new FontFamily("宋体");
  129. System.Drawing.Font myFont = new Font(fontFamily, 9);
  130. // Determine the color of the brush to draw each item based on the index of the item to draw.
  131. if((e.State & DrawItemState.Selected) == DrawItemState.Selected)
  132. {
  133. //e.Graphics.FillRectangle(Brushes.Blue, e.Bounds);
  134. if(e.Index > -1)
  135. {
  136. e.Graphics.DrawString(listBox1.Items[e.Index].ToString(), myFont, Brushes.White, e.Bounds, StringFormat.GenericDefault);
  137. }
  138. }
  139. else
  140. {
  141. //e.Graphics.FillRectangle(Brushes.White, e.Bounds);
  142. if(e.Index > -1)
  143. {
  144. e.Graphics.DrawString(listBox1.Items[e.Index].ToString(), myFont, myBrush, e.Bounds, StringFormat.GenericDefault);
  145. }
  146. }
  147. // Draw the current item text based on the current Font and the custom brush settings.
  148. //e.Graphics.DrawString(listBox1.Items[e.Index].ToString(), e.Font, myBrush, e.Bounds, StringFormat.GenericDefault);
  149. // If the ListBox has focus, draw a focus rectangle around the selected item.
  150. e.DrawFocusRectangle();
  151. }
  152. #endregion

转载来自:http://blog.csdn.net/Rock870210/article/details/5856235

在Winfrom下实现类似百度、Google搜索自能提示功能的更多相关文章

  1. 【搜索引擎】 PostgreSQL 10 实时全文检索和分词、相似搜索、模糊匹配实现类似Google搜索自动提示

    需求分析 要通过PostgreSQL实现类似Google搜索自动提示的功能,例如要实现一个查询海量数据中的商品名字,每次输入就提示用户各种相关搜索选项,例如淘宝.京东等电商查询 思路 这个功能可以用 ...

  2. Jquery实现类似百度的搜索框

    最近工作中需要做一个搜索框,类似百度的搜索框,需要达到两个功能: 1.输入关键字,展示匹配的下拉列表 2.选择匹配的项后查出相关内容 一般电商网站中也经常用到该搜索条,首先分析功能实现,输入关键字马上 ...

  3. ajax+JQuery实现类似百度智能搜索框

    最近再学习ajax,上课老师让我们实现一个类似百度首页实现搜索框的功能,刚开始做的时候没有一点头绪,查阅大量网上的资源后,发现之前的与我们现在的有些区别,所以在此写出来,希望能对大家有所帮助. 下面先 ...

  4. C#winform抓取百度,Google搜索关键词结果

    基于网站seo,做了一采集百度和Google搜索关键字结果的采集.在这里与大家分享一下 先看先效果图 代码附加:  1   private void baidu_Click(object sender ...

  5. Jquery实现搜索框提示功能

    博客的前某一篇文章中http://www.cnblogs.com/hEnius/p/2013-07-01.html写过一个用Ajax来实现一个文本框输入的提示功能.最近在一个管理项目的项目中,使用后发 ...

  6. **IOS自动完成(搜索自动提示)功能实现

    UISearchBar搜索AutoComplete下拉列表搜索提示 http://www.codeios.com/thread-10685-1-1.html 介绍:     在搜索框上加入下拉列表.在 ...

  7. AJAX实现类似百度的搜索提示,自动补全和键盘、鼠标操作

    <script type="text/javascript"> $(document).ready(function(){ var highlightIndex = - ...

  8. 使用jqueryUI和corethink实现的类似百度的搜索提示

    代码:http://download.csdn.net/detail/u012995856/9676845 效果: 目录: 这里是以corethink模块的形式,只需要安装上访问 index.php? ...

  9. vue单页面条件下添加类似浏览器的标签页切换功能

    在用vue开发的时候,单页面应用程序,而又有标签页这种需求,各种方式实现不了, 从这个 到这个,然后再返回上面那个 因为每个标签页的route不一样,导致组件重新渲染的问题,怎么都不知道如何实现... ...

随机推荐

  1. bat 常用命令

    基础部分:======================================================================一.基础语法: 1.批处理文件是一个". ...

  2. Chapter 1 First Sight——8

    It took only one trip to get all my stuff upstairs. 就一趟我就把所有的东西搬到楼上了. I got the west bedroom that fa ...

  3. WebDriver(Selenium2) 处理可能存在的JS弹出框

    http://uniquepig.iteye.com/blog/1703103 在自动化测试过程中,有些情况下我们会遇到一些潜在的Javascript弹出框.(即某些条件下才会出现,不是固定出现),然 ...

  4. Linux iptables 防火墙详解

    0x00 iptables介绍 linux的包过滤功能,即linux防火墙,它由netfilter 和 iptables 两个组件组成. netfilter 组件也称为内核空间,是内核的一部分,由一些 ...

  5. (四)Jquery Mobile表单

    Jquery Mobile表单与列表 一.JM表单      1.表单      普通html表单            效果:          2.只能输入数字的表单           效果:  ...

  6. xcode 6 出现的新问题

    1.prefix.pch文件的使用 [1].需要自己创建 点击new file-->选择IOS中的Other选项卡,选择PCH File [2].创建完后需要设置一下才能成功 Prefix He ...

  7. 做自己的串口调试工具-MFC

    之前一共说了几个软件库,串口通讯的,xml加载的,还有动态提示的,通过这三个库就可以实现一个自己的串口调试工具,成品如下 动态加载配置文件之后如下 软件可以在xml中配置发送的字符串,不算是大工程,但 ...

  8. CodeForces 622B The Time

    水题. #include <stdio.h> #include <algorithm> #include <string.h> #include <queue ...

  9. 将UTF8编码转化为中文 - NSString方法

    方法一: 代码如下,如有更好的方法 麻烦贴出来,这个方法是通过webview进行解码的 UIWebView *web = [[UIWebView alloc] init]; NSString *tsw ...

  10. Laravel 使用简述

    开始使用laravel 在此之前我们需要修改一些配置: app/config/app.php 文件中的 debug 选项设置为 true (注:开启开发模式,更友好的开发提示:网站运行时则关闭): a ...