TextBox自动完成控件
2014-11-12 16:50
162 查看
using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; using System.Data; using System.Text; using System.Drawing; using System.Drawing.Imaging; using System.Windows.Forms; namespace LHXETDDataManagement.Controls { /// <summary> /// TextBox扩展类 /// </summary> public class AutoCompleteTextBox : System.Windows.Forms.TextBox { private bool _isSelect = false; private ListBox _lboxSearch = null; public AutoCompleteTextBox() { this.DataSouce = new List<string>(); this.PanelMaxHeight = 130; //10行 } /// <summary> /// 获取或设置自动完成数据源 /// </summary> public List<string> DataSouce { set; get; } /// <summary> /// 获取或设置面版最大高度 /// </summary> public int PanelMaxHeight { set; get; } protected override void OnKeyPress(KeyPressEventArgs e) { _isSelect = false; base.OnKeyPress(e); } protected override void OnKeyDown(KeyEventArgs e) { if (_lboxSearch == null) { return; } int index = _lboxSearch.SelectedIndex; switch (e.KeyCode) { case Keys.Up: index--; break; case Keys.Down: index++; break; case Keys.Enter: LboxSearch_Click(_lboxSearch, new EventArgs()); break; } if (index < 0) { index = 0; } if (index >= _lboxSearch.Items.Count) { index = _lboxSearch.Items.Count - 1; } _lboxSearch.SelectedIndex = index; base.OnKeyDown(e); } protected override void OnTextChanged(EventArgs e) { base.OnTextChanged(e); Control parent = this.Parent; string text = this.Text.Trim(); if (_lboxSearch != null) { parent.Controls.Remove(_lboxSearch); } if (string.IsNullOrEmpty(text) || DataSouce == null || DataSouce.Count == 0) { return; } List<string> list = DataSouce.Where(p => p.Contains(text)).ToList(); if (list.Count > 0 && !_isSelect) { int height = list.Count * 13; if (height >= PanelMaxHeight) { height = PanelMaxHeight; } _lboxSearch = new ListBox(); _lboxSearch.Location = new Point(this.Location.X, this.Location.Y + this.Size.Height); _lboxSearch.Size = new Size(this.Size.Width, height); _lboxSearch.Items.AddRange(list.ToArray()); _lboxSearch.Click += new EventHandler(LboxSearch_Click); parent.Controls.Add(_lboxSearch); //置顶 _lboxSearch.BringToFront(); } } private void LboxSearch_Click(object sender, EventArgs e) { if (_lboxSearch != null) { _isSelect = true; this.Text = _lboxSearch.Text; this.Focus(); this.SelectionStart = this.Text.Length; } } } }
相关文章推荐
- winform中complete textbox,下拉自动完成控件
- 老鼠Atlas之旅(八):Hands-On Lab 4第四个实验,Atlas服务器控件自动完成
- 用Atlas创建个自动完成的textbox
- 在浏览器控件中启用自动完成功能
- 重写TextBox控件,使其支持各种数字、邮件等自动验证功能
- Autocomplete Textbox Example javascript实现自动完成成功
- 自动适应输入内容高度的TextBox控件(摘自罗永浩全集)
- 具有Ajax自动建议功能的TextBox控件(附源码)
- AutoComplete Extender控件-----实现自动完成功能
- 在浏览器控件中启用自动完成功能
- 在浏览器控件中启用自动完成功能
- 怎样用验证控件RangeValidator1来验证datagrid自动生成的textbox控件??
- 自动完成的TextBox,类似Windows的运行框
- textbox显示下拉框中,实现自动完成
- 露雨资源库三(第一个.net2.0软件)-为控件增加自动完成功能
- 自己空余時間完成的textbox控件
- Silverlight AutoCompleteBox(自动完成输入框控件)使用方法
- AJAX自动完成显示控件AutoComplete的使用
- textBOX验证控件,自动验证数据正确与否!只需选择你的数据类型
- 最近做的一个类似Google自动完成的服务器控件