WinForm 自动完成控件实例代码简析
更新时间:2020年6月25日 11:43 点击:2177
在Web的应用方面有js的插件实现自动完成(或叫智能提示)功能,但在WinForm窗体应用方面就没那么好了。
TextBox控件本身是提供了一个自动提示功能,只要用上这三个属性:
AutoCompleteCustomSource:AutoCompleteSource 属性设置为CustomSource 时要使用的 StringCollection。
AutoCompleteMode:指示文本框的文本完成行为。
AutoCompleteSource:自动完成源,可以是 AutoCompleteSource 的枚举值之一。
就行了, 一个简单的示例如下
textBox1.AutoCompleteCustomSource .AddRange(new string[] { "java","javascript","js","c#","c","c++" });
textBox1.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
textBox1.AutoCompleteSource = AutoCompleteSource.CustomSource;
可是这种方式的不支持我们中文的简拼自动完成(如在文本框里输入"gz"就会出现"广州")。只好自己写一个支持简拼自动完成的控件了。
这是效果图
控件不太复杂,一个TextBox和一个ListBox。代码方面,用DataTable作数据源,每次在TextBox的值时,通过DataTable的Select方法,配上合适的表达式(如:{0} like '{1}%' and IsNull([{2}], ' ') <> ' ')来筛选出合适的备选文本内容,以下则是控件的代码:
private TextBox _tb;
private ListBox _lb;
private DataTable _dt_datasource;
private bool _text_lock;
private string _general_text;//原始输入文本框的值
private bool _lb_kd_first_top;//listbox是否第一次到达顶部
private int _itemCount;
/// <summary>
/// TextBox的Text属性,增加了_text_lock操作,放置触发TextChanged事件
/// </summary>
private string TextBoxText
{
get { return _tb.Text; }
set
{
_text_lock = true;
_tb.Text = value;
_text_lock = false;
}
}
/// <summary>
/// 显示在ListBox的字段名
/// </summary>
public string ValueName { get; set; }
/// <summary>
/// 用于匹配的字段名
/// </summary>
public string CodeName { get; set; }
/// <summary>
/// 显示提示项的数量
/// </summary>
public int ItemCount
{
get
{ return _itemCount; }
set
{
if (value <= 0)
_itemCount = 1;
else
_itemCount = value;
}
}
public DataTable DataSource
{
get { return _dt_datasource; }
set { _dt_datasource = value; }
}
public AutoComplete()
{
InitialControls();
}
void AutoComplete_Load(object sender, EventArgs e)
{
_tb.Width = this.Width;
_lb.Width = _tb.Width;
this.Height = _tb.Height-1;
}
void AutoComplete_LostFocus(object sender, EventArgs e)
{
_lb.Visible = false;
this.Height = _tb.Height-1;
}
//列表框按键事件
void _lb_KeyDown(object sender, KeyEventArgs e)
{
if (_lb.Items.Count == 0 || !_lb.Visible) return;
if (!_lb_kd_first_top && ((e.KeyCode == Keys.Up && _lb.SelectedIndex == 0) || (e.KeyCode == Keys.Down && _lb.SelectedIndex == _lb.Items.Count)))
{
_lb.SelectedIndex = -1;
TextBoxText = _general_text;
}
else
{
TextBoxText = ((DataRowView)_lb.SelectedItem)[ValueName].ToString();
_lb_kd_first_top = _lb.SelectedIndex != 0;
}
if (e.KeyCode == Keys.Enter && _lb.SelectedIndex != -1)
{
_lb.Visible = false;
this.Height = _tb.Height;
_tb.Focus();
}
}
//列表鼠标单击事件
void _lb_Click(object sender, EventArgs e)
{
if (_lb.SelectedIndex != -1)
{
TextBoxText = ((DataRowView)_lb.SelectedItem)[ValueName].ToString();
}
_lb.Visible = false;
_tb.Focus();
this.Height = _tb.Height;
}
//文本框按键事件
void _tb_KeyDown(object sender, KeyEventArgs e)
{
if (_lb.Items.Count == 0||!_lb.Visible) return;
bool _is_set = false;
if (e.KeyCode == Keys.Up)
{
if (_lb.SelectedIndex <= 0)
{
_lb.SelectedIndex = -1;
TextBoxText = _general_text;
}
else
{
_lb.SelectedIndex--;
_is_set = true;
}
}
else if (e.KeyCode == Keys.Down)
{
if (_lb.SelectedIndex == _lb.Items.Count - 1)
{
_lb.SelectedIndex = 0;
_lb.SelectedIndex = -1;
TextBoxText = _general_text;
}
else
{
_lb.SelectedIndex++;
_is_set = true;
}
}
else if (e.KeyCode == Keys.Enter)
{
_lb.Visible = false;
this.Height = _tb.Height;
_is_set = _lb.SelectedIndex != -1;
}
_lb_kd_first_top = _lb.SelectedIndex != 0;
if (_is_set)
{
_text_lock = true;
_tb.Text = ((DataRowView)_lb.SelectedItem)[ValueName].ToString();
_tb.SelectionStart = _tb.Text.Length + 10;
_tb.SelectionLength = 0;
_text_lock = false;
}
}
//文本框文本变更事件
void _tb_TextChanged(object sender, EventArgs e)
{
if (_text_lock) return;
_general_text = _tb.Text;
_lb.Visible = true;
_lb.Height = _lb.ItemHeight * (_itemCount+1);
this.BringToFront();
_lb.BringToFront();
this.Height = _tb.Height + _lb.Height;
DataTable temp_table = _dt_datasource.Clone();
string filtStr = FormatStr(_tb.Text);
DataRow [] rows = _dt_datasource.Select(string.Format(GetFilterStr(),CodeName,filtStr,_lb.DisplayMember));
for (int i = 0; i < rows.Length&&i<_itemCount; i++)
{
temp_table.Rows.Add(rows[i].ItemArray);
}
_lb.DataSource = temp_table;
if (_lb.Items.Count > 0) _lb.SelectedItem = _lb.Items[0];
}
/// <summary>
/// 初始化控件
/// </summary>
private void InitialControls()
{
_lb_kd_first_top = true;
_tb = new TextBox();
_tb.Location = new Point(0, 0);
_tb.Margin = new System.Windows.Forms.Padding(0);
_tb.Width = this.Width;
_tb.TextChanged += new EventHandler(_tb_TextChanged);
_tb.KeyUp += new KeyEventHandler(_tb_KeyDown);
_lb = new ListBox();
_lb.Visible = false;
_lb.Width = _tb.Width;
_lb.Margin = new System.Windows.Forms.Padding(0);
_lb.DisplayMember = ValueName;
_lb.SelectionMode = SelectionMode.One;
_lb.Location = new Point(0, _tb.Height);
_lb.KeyUp += new KeyEventHandler(_lb_KeyDown);
_lb.Click += new EventHandler(_lb_Click);
this.Controls.Add(_tb);
this.Controls.Add(_lb);
this.Height = _tb.Height - 1;
this.LostFocus += new EventHandler(AutoComplete_LostFocus);
this.Leave += new EventHandler(AutoComplete_LostFocus);
this.Load += new EventHandler(AutoComplete_Load);
}
/// <summary>
/// 获取过滤格式字符串
/// </summary>
/// <returns></returns>
private string GetFilterStr()
{
//未过滤注入的字符 ' ] %任意 *任意
string filter = " {0} like '{1}%' and IsNull([{2}], ' ') <> ' ' ";
if (_dt_datasource.Rows[0][CodeName].ToString().LastIndexOf(';') > -1)
filter = " {0} like '%;{1}%' and IsNull([{2}],' ') <> ' ' ";
return filter;
}
/// <summary>
/// 过滤字符串中一些可能造成出错的字符
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
private string FormatStr(string str)
{
if (string.IsNullOrEmpty(str)) return string.Empty;
str = str.Replace("[", "[[]").Replace("%", "[%]").Replace("*", "[*]").Replace("'", "''");
if (CodeName == "code") str = str.Replace(" ", "");
return str;
}
下面是使用控件的例子
class Common
{
/// <summary>
/// 生成测试数据源
/// </summary>
public static DataTable CreateTestDataSoucre
{
get
{
List<KeyValuePair<string, string>> source = new List<KeyValuePair<string, string>>()
{
new KeyValuePair<string,string>("张三",";zs;张三;"),
new KeyValuePair<string,string>("李四",";li;李四;"),
new KeyValuePair<string,string>("王五",";ww;王五;"),
new KeyValuePair<string,string>("赵六",";zl;赵六;"),
new KeyValuePair<string,string>("洗刷",";cs;csharp;c#;洗刷;"),
new KeyValuePair<string,string>("爪哇",";java;爪哇;"),
new KeyValuePair<string,string>("java",";java;"),
new KeyValuePair<string,string>("c#",";c#;cs;csharp;"),
new KeyValuePair<string,string>("javascript",";javascript;js;")
};
DataTable table = new DataTable();
table.Columns.Add("id");
table.Columns.Add("name");
table.Columns.Add("code");
for (int i = 0; i < source.Count; i++)
{
DataRow row = table.Rows.Add();
row["id"] = i;
row["name"] = source[i].Key;
row["code"] = source[i].Value;
}
return table;
}
}
}
//.............
AutoComplete ac=new AutoComplete();
ac.ValueName = "name";
ac.CodeName = "code";
ac.DataSource= Common.CreateTestDataSoucre;
ac.ItemCount= 5;
TextBox控件本身是提供了一个自动提示功能,只要用上这三个属性:
AutoCompleteCustomSource:AutoCompleteSource 属性设置为CustomSource 时要使用的 StringCollection。
AutoCompleteMode:指示文本框的文本完成行为。
AutoCompleteSource:自动完成源,可以是 AutoCompleteSource 的枚举值之一。
就行了, 一个简单的示例如下
复制代码 代码如下:
textBox1.AutoCompleteCustomSource .AddRange(new string[] { "java","javascript","js","c#","c","c++" });
textBox1.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
textBox1.AutoCompleteSource = AutoCompleteSource.CustomSource;
可是这种方式的不支持我们中文的简拼自动完成(如在文本框里输入"gz"就会出现"广州")。只好自己写一个支持简拼自动完成的控件了。
这是效果图
控件不太复杂,一个TextBox和一个ListBox。代码方面,用DataTable作数据源,每次在TextBox的值时,通过DataTable的Select方法,配上合适的表达式(如:{0} like '{1}%' and IsNull([{2}], ' ') <> ' ')来筛选出合适的备选文本内容,以下则是控件的代码:
复制代码 代码如下:
private TextBox _tb;
private ListBox _lb;
private DataTable _dt_datasource;
private bool _text_lock;
private string _general_text;//原始输入文本框的值
private bool _lb_kd_first_top;//listbox是否第一次到达顶部
private int _itemCount;
复制代码 代码如下:
/// <summary>
/// TextBox的Text属性,增加了_text_lock操作,放置触发TextChanged事件
/// </summary>
private string TextBoxText
{
get { return _tb.Text; }
set
{
_text_lock = true;
_tb.Text = value;
_text_lock = false;
}
}
/// <summary>
/// 显示在ListBox的字段名
/// </summary>
public string ValueName { get; set; }
/// <summary>
/// 用于匹配的字段名
/// </summary>
public string CodeName { get; set; }
/// <summary>
/// 显示提示项的数量
/// </summary>
public int ItemCount
{
get
{ return _itemCount; }
set
{
if (value <= 0)
_itemCount = 1;
else
_itemCount = value;
}
}
public DataTable DataSource
{
get { return _dt_datasource; }
set { _dt_datasource = value; }
}
复制代码 代码如下:
public AutoComplete()
{
InitialControls();
}
复制代码 代码如下:
void AutoComplete_Load(object sender, EventArgs e)
{
_tb.Width = this.Width;
_lb.Width = _tb.Width;
this.Height = _tb.Height-1;
}
void AutoComplete_LostFocus(object sender, EventArgs e)
{
_lb.Visible = false;
this.Height = _tb.Height-1;
}
复制代码 代码如下:
//列表框按键事件
void _lb_KeyDown(object sender, KeyEventArgs e)
{
if (_lb.Items.Count == 0 || !_lb.Visible) return;
if (!_lb_kd_first_top && ((e.KeyCode == Keys.Up && _lb.SelectedIndex == 0) || (e.KeyCode == Keys.Down && _lb.SelectedIndex == _lb.Items.Count)))
{
_lb.SelectedIndex = -1;
TextBoxText = _general_text;
}
else
{
TextBoxText = ((DataRowView)_lb.SelectedItem)[ValueName].ToString();
_lb_kd_first_top = _lb.SelectedIndex != 0;
}
if (e.KeyCode == Keys.Enter && _lb.SelectedIndex != -1)
{
_lb.Visible = false;
this.Height = _tb.Height;
_tb.Focus();
}
}
//列表鼠标单击事件
void _lb_Click(object sender, EventArgs e)
{
if (_lb.SelectedIndex != -1)
{
TextBoxText = ((DataRowView)_lb.SelectedItem)[ValueName].ToString();
}
_lb.Visible = false;
_tb.Focus();
this.Height = _tb.Height;
}
复制代码 代码如下:
//文本框按键事件
void _tb_KeyDown(object sender, KeyEventArgs e)
{
if (_lb.Items.Count == 0||!_lb.Visible) return;
bool _is_set = false;
if (e.KeyCode == Keys.Up)
{
if (_lb.SelectedIndex <= 0)
{
_lb.SelectedIndex = -1;
TextBoxText = _general_text;
}
else
{
_lb.SelectedIndex--;
_is_set = true;
}
}
else if (e.KeyCode == Keys.Down)
{
if (_lb.SelectedIndex == _lb.Items.Count - 1)
{
_lb.SelectedIndex = 0;
_lb.SelectedIndex = -1;
TextBoxText = _general_text;
}
else
{
_lb.SelectedIndex++;
_is_set = true;
}
}
else if (e.KeyCode == Keys.Enter)
{
_lb.Visible = false;
this.Height = _tb.Height;
_is_set = _lb.SelectedIndex != -1;
}
_lb_kd_first_top = _lb.SelectedIndex != 0;
if (_is_set)
{
_text_lock = true;
_tb.Text = ((DataRowView)_lb.SelectedItem)[ValueName].ToString();
_tb.SelectionStart = _tb.Text.Length + 10;
_tb.SelectionLength = 0;
_text_lock = false;
}
}
//文本框文本变更事件
void _tb_TextChanged(object sender, EventArgs e)
{
if (_text_lock) return;
_general_text = _tb.Text;
_lb.Visible = true;
_lb.Height = _lb.ItemHeight * (_itemCount+1);
this.BringToFront();
_lb.BringToFront();
this.Height = _tb.Height + _lb.Height;
DataTable temp_table = _dt_datasource.Clone();
string filtStr = FormatStr(_tb.Text);
DataRow [] rows = _dt_datasource.Select(string.Format(GetFilterStr(),CodeName,filtStr,_lb.DisplayMember));
for (int i = 0; i < rows.Length&&i<_itemCount; i++)
{
temp_table.Rows.Add(rows[i].ItemArray);
}
_lb.DataSource = temp_table;
if (_lb.Items.Count > 0) _lb.SelectedItem = _lb.Items[0];
}
复制代码 代码如下:
/// <summary>
/// 初始化控件
/// </summary>
private void InitialControls()
{
_lb_kd_first_top = true;
_tb = new TextBox();
_tb.Location = new Point(0, 0);
_tb.Margin = new System.Windows.Forms.Padding(0);
_tb.Width = this.Width;
_tb.TextChanged += new EventHandler(_tb_TextChanged);
_tb.KeyUp += new KeyEventHandler(_tb_KeyDown);
_lb = new ListBox();
_lb.Visible = false;
_lb.Width = _tb.Width;
_lb.Margin = new System.Windows.Forms.Padding(0);
_lb.DisplayMember = ValueName;
_lb.SelectionMode = SelectionMode.One;
_lb.Location = new Point(0, _tb.Height);
_lb.KeyUp += new KeyEventHandler(_lb_KeyDown);
_lb.Click += new EventHandler(_lb_Click);
this.Controls.Add(_tb);
this.Controls.Add(_lb);
this.Height = _tb.Height - 1;
this.LostFocus += new EventHandler(AutoComplete_LostFocus);
this.Leave += new EventHandler(AutoComplete_LostFocus);
this.Load += new EventHandler(AutoComplete_Load);
}
/// <summary>
/// 获取过滤格式字符串
/// </summary>
/// <returns></returns>
private string GetFilterStr()
{
//未过滤注入的字符 ' ] %任意 *任意
string filter = " {0} like '{1}%' and IsNull([{2}], ' ') <> ' ' ";
if (_dt_datasource.Rows[0][CodeName].ToString().LastIndexOf(';') > -1)
filter = " {0} like '%;{1}%' and IsNull([{2}],' ') <> ' ' ";
return filter;
}
/// <summary>
/// 过滤字符串中一些可能造成出错的字符
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
private string FormatStr(string str)
{
if (string.IsNullOrEmpty(str)) return string.Empty;
str = str.Replace("[", "[[]").Replace("%", "[%]").Replace("*", "[*]").Replace("'", "''");
if (CodeName == "code") str = str.Replace(" ", "");
return str;
}
下面是使用控件的例子
复制代码 代码如下:
class Common
{
/// <summary>
/// 生成测试数据源
/// </summary>
public static DataTable CreateTestDataSoucre
{
get
{
List<KeyValuePair<string, string>> source = new List<KeyValuePair<string, string>>()
{
new KeyValuePair<string,string>("张三",";zs;张三;"),
new KeyValuePair<string,string>("李四",";li;李四;"),
new KeyValuePair<string,string>("王五",";ww;王五;"),
new KeyValuePair<string,string>("赵六",";zl;赵六;"),
new KeyValuePair<string,string>("洗刷",";cs;csharp;c#;洗刷;"),
new KeyValuePair<string,string>("爪哇",";java;爪哇;"),
new KeyValuePair<string,string>("java",";java;"),
new KeyValuePair<string,string>("c#",";c#;cs;csharp;"),
new KeyValuePair<string,string>("javascript",";javascript;js;")
};
DataTable table = new DataTable();
table.Columns.Add("id");
table.Columns.Add("name");
table.Columns.Add("code");
for (int i = 0; i < source.Count; i++)
{
DataRow row = table.Rows.Add();
row["id"] = i;
row["name"] = source[i].Key;
row["code"] = source[i].Value;
}
return table;
}
}
}
//.............
AutoComplete ac=new AutoComplete();
ac.ValueName = "name";
ac.CodeName = "code";
ac.DataSource= Common.CreateTestDataSoucre;
ac.ItemCount= 5;
相关文章
- 这篇文章主要给大家介绍了关于C#创建自定义控件及添加自定义属性和事件使用的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用C#具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧...2020-06-25
- 本篇文章是对C#中WinForm控件之Dock顺序调整进行了详细的分析介绍,需要的朋友参考下...2020-06-25
- 这篇文章主要介绍了C#实现跨线程操作控件方法,主要采用异步访问方式实现,需要的朋友可以参考下...2020-06-25
- 这篇文章主要介绍了C# Winform中实现主窗口打开登录窗口关闭的方法,这在需要用户名密码的软件项目中是必用的一个技巧,要的朋友可以参考下...2020-06-25
- 下面小编就为大家带来一篇C# winform打开Excel文档的方法总结(必看篇)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧...2020-06-25
- 这篇文章主要给大家介绍C# winform快捷键设置技巧,涉及到C winform快捷键相关知识,对C winform知识感兴趣的朋友可以参考下本篇文章...2020-06-25
c#中Winform实现多线程异步更新UI(进度及状态信息)
本篇文章主要介绍了c#中Winform实现多线程异步更新UI(进度及状态信息) ,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧...2020-06-25- 这篇文章主要介绍了C# 如何设置label(标签)控件的背景颜色为透明,帮助大家更好的理解和使用c#,感兴趣的朋友可以了解下...2020-12-08
- 这篇文章主要给大家介绍了关于c# winform异步不卡界面的实现方法,文中通过示例代码介绍的非常详细,对大家学习或者使用c#具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧...2020-06-25
- 本篇文章是对C#中自定义控件的制作与使用实例进行了详细的分析介绍,需要的朋友参考下...2020-06-25
C# WinForm程序处理后台繁忙导致前台控件假死现象解决方法
这篇文章主要介绍了C# WinForm程序处理后台繁忙导致前台控件假死现象解决方法,本文通过Application.DoEvents()解决这个问题,并讲解了Application.DoEvents()的作用,需要的朋友可以参考下...2020-06-25- 这篇文章主要介绍了C#中winform使用相对路径读取文件的方法,实例分析了C#使用相对路径读取文件的技巧与实际应用,需要的朋友可以参考下...2020-06-25
- 这篇文章主要为大家详细介绍了Bootstrap树形控件使用方法,感兴趣的小伙伴们可以参考一下...2016-01-29
- 本文介绍了jQuery实现用户输入自动完成功能的方法。具有很好的参考价值,下面跟着小编一起来看下吧...2017-02-19
- 这篇文章主要介绍了C#实现根据指定容器和控件名字获得控件的方法,其中包括了遍历与递归的应用,需要的朋友可以参考下...2020-06-25
学习使用bootstarp基本控件(table、form、button)
这篇文章主要教会大家学习使用bootstarp基本控件,如table、form、button控件,感兴趣的小伙伴们可以参考一下...2016-04-16Repeater事件OnItemCommand取得行内控件的方法
这篇文章主要介绍了Repeater事件OnItemCommand取得行内控件的方法,有需要的朋友可以参考一下...2021-09-22- 这篇文章主要介绍了C#多线程与跨线程访问界面控件的方法,实例分析了C#多线程与跨线程访问空间的技巧,具有一定参考借鉴价值,需要的朋友可以参考下...2020-06-25
winform中的ListBox和ComboBox绑定数据用法实例
这篇文章主要介绍了winform中的ListBox和ComboBox绑定数据用法,实例分析了将集合数据绑定到ListBox和ComboBox控件的技巧,具有一定参考借鉴价值,需要的朋友可以参考下...2020-06-25- 这篇文章主要介绍了C#在Winform开发中使用Grid++报表,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-06-25