代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
CSharp
】
Winfrom下实现类似百度、Google搜索智能提示功能
作者:
rock870210
/ 发布于
2014/6/26
/
827
数据源来自页面的一个ComboBox的数据源List
集合 页面放置一个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
试试其它关键字
Winfrom
智能提示
提示功能
同语言下
.
文件IO 操作类库
.
Check图片类型[JPEG(.jpg 、.jpeg),TIF,GIF,BMP,PNG,P
.
机器名和IP取得(IPV4 IPV6)
.
Tiff转换Bitmap
.
linqHelper
.
MadieHelper.cs
.
RegHelper.cs
.
如果关闭一个窗体后激活另一个窗体的事件或方法
.
创建日志通用类
.
串口辅助开发类
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
rock870210
贡献的其它代码
(
12
)
.
定时发送邮件
.
页面版文本框智能提示
.
页面刷新后或回发后回到原始位置
.
Winfrom下实现类似百度、Google搜索智能提示功能
.
获取MAC、IP、机器名
.
自定义函数
.
删除默认约束存储过程及增加、修改、删除字段
.
简体中文-繁体互相转换
.
监控页面刷新
.
科学计算器
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3