码迷,mamicode.com
首页 > Windows程序 > 详细

在Winfrom下实现类似百度、Google搜索自能提示功能

时间:2016-02-16 18:57:14      阅读:243      评论:0      收藏:0      [点我收藏+]

标签:

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

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

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

 

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

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

在Winfrom下实现类似百度、Google搜索自能提示功能

标签:

原文地址:http://www.cnblogs.com/wlming/p/5193095.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!