The previous article is to use a ComboBox inside the original event to achieve fuzzy query, the operation is more flexible, but received comments that the use of AutoComplete attributes can be used to achieve fuzzy query, but according to my understanding, autocomplete although can easily realize fuzzy query, However, there is a certain flaw, that is, fuzzy query can only go from left to right.
Previous link Address: http://www.cnblogs.com/xilipu31/p/3993049.html
Here's a simple way to implement it:
Front Desk: A simple form +combobox control
Backstage: assert that list<string> listonit is used to initialize the alternate data for the ComboBox and then set the ComboBox property: AutoCompleteSource (auto-complete data source), AutoCompleteMode ( Prompt type) and AutoCompleteCustomSource (bound data source).
The specific code is as follows:
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950 |
using
System;
using
System.Collections.Generic;
using
System.ComponentModel;
using
System.Data;
using
System.Drawing;
using
System.Linq;
using
System.Text;
using
System.Windows.Forms; namespace
TimerDemo
{
public
partial
class
Form3 : Form
{
//初始化绑定默认关键词(此数据源可以从数据库取)
List<
string
> listOnit =
new
List<
string
>();
public
Form3()
{
InitializeComponent();
}
private
void
Form3_Load(
object
sender, EventArgs e)
{
BindComboBox();
}
/// <summary>
/// 绑定ComboBox
/// </summary>
private
void
BindComboBox()
{
listOnit.Add(
"张三"
);
listOnit.Add(
"张思"
);
listOnit.Add(
"张五"
);
listOnit.Add(
"王五"
);
listOnit.Add(
"刘宇"
);
listOnit.Add(
"马六"
);
listOnit.Add(
"孙楠"
);
listOnit.Add(
"那英"
);
listOnit.Add(
"刘欢"
);
//自动完成数据源
this
.comboBox1.AutoCompleteSource = AutoCompleteSource.CustomSource;
//提示类型 建议列表+自动补全
this
.comboBox1.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
//绑定数据源
this
.comboBox1.AutoCompleteCustomSource.AddRange(listOnit.ToArray());
}
}
}
|
The following are the implementation effects:
Input keyword fuzzy query from left to right (for example, input: Zhang)
The correct hints and results can be drawn.
What if it's not a fuzzy query from left to right? (e.g. input: three)
It can be seen that fuzzy data cannot be queried.
Summarize:
ComboBox Control AutoComplete event can be used in the Fuzzy query degree is not high, left-to-right keyword search, if you want to implement advanced fuzzy query, or their own definition of the way more flexible.
WinForm in a ComboBox using AutoComplete property to implement Fuzzy query (defective)