我的上一个支持拼音检索的TextBox扩展控件,由于有些网友留言和发邮件问如何用,
如:菜鸟AAA等当时想到肯定大家都会用,就没上传示例程序。
原文为:http://www.cnblogs.com/whitewolf/archive/2009/12/03/1615975.html#1717373
既然有人问,那就上传下是下程序。肯定很多人都知道如何用,请越过就是,不要发弁言。
主要只有三个属性暴露出来:
1:MaxItemCout:这是在多个下拉条时,显示多少条可见;
2:SearchMode:检索方式:只提供了SearchMode.Contains 和SearchMode.StartWith包含和以其开始区别;
3:SpellSearchSource:检索的数据源,仅显现的中文等字符;
主要在它上:有几种方式设计:
1:设计时:
代码
2:代码中;
一般的:
//一般的CODE为;
spellSearchBoxEx1.SpellSearchSource = new string[] { "中国", "中国fgdfs", "中阿收费的肌肤", "男生", "女生" };
3:数据库情况下,本实例用的是随机生成中文模拟数据库情况;
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
//一般的CODE为;
spellSearchBoxEx1.SpellSearchSource = new string[] { "中国", "中国fgdfs", "中阿收费的肌肤", "男生", "女生" };
// 连接数据库情况下仅此;
spellSearchBoxExBySql.SpellSearchSource = GetSpellBoxSource(GetDataTable());
}
public string[] GetSpellBoxSource(DataTable dt)
{
List<string> list = new List<string>();
foreach (DataRow dr in dt.Rows)
{
if (!Convert.IsDBNull(dr["Text"]))
list.Add(dr["Text"].ToString());
}
return list.ToArray();
}
public DataTable GetDataTable()
{
//这里是自己的代码连接数据库仅得到要填的列;
//本方法没连数据库,用随机生成中文模拟获得DatatTable
// DataTable dt = DB.GetDatatable("sql");
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("Text", typeof(string)));
Random rn = new Random();
for (int i = 0; i < 50; i++)
{
string str ="中" +GetRandomChinese(rn.Next(8));
DataRow dr = dt.NewRow();
dr["Text"] = str;
dt.Rows.Add(dr);
}
return dt;
}
#region 以下只是随机生成中文,与本控件使用无关;
public string GetRandomChinese(int strlength)
{
Encoding gb = Encoding.GetEncoding("gb2312");
object[] bytes = this.CreateRegionCode(strlength);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < strlength; i++)
{
string temp = gb.GetString((byte[])Convert.ChangeType(bytes[i], typeof(byte[])));
sb.Append(temp);
}
return sb.ToString();
}
private object[] CreateRegionCode(int strlength)
{
//定义一个字符串数组储存汉字编码的组成元素
string[] rBase = new String[16] { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f" };
Random rnd = new Random();
object[] bytes = new object[strlength];
for (int i = 0; i < strlength; i++)
{
int r1 = rnd.Next(11, 14);
string str_r1 = rBase[r1].Trim();
rnd = new Random(r1 * unchecked((int)DateTime.Now.Ticks) + i);
int r2;
if (r1 == 13)
{
r2 = rnd.Next(0, 7);
}
else
{
r2 = rnd.Next(0, 16);
}
string str_r2 = rBase[r2].Trim();
rnd = new Random(r2 * unchecked((int)DateTime.Now.Ticks) + i);
int r3 = rnd.Next(10, 16);
string str_r3 = rBase[r3].Trim();
rnd = new Random(r3 * unchecked((int)DateTime.Now.Ticks) + i);
int r4;
if (r3 == 10)
{
r4 = rnd.Next(1, 16);
}
else if (r3 == 15)
{
r4 = rnd.Next(0, 15);
}
else
{
r4 = rnd.Next(0, 16);
}
string str_r4 = rBase[r4].Trim();
byte byte1 = Convert.ToByte(str_r1 + str_r2, 16);
byte byte2 = Convert.ToByte(str_r3 + str_r4, 16);
byte[] str_r = new byte[] { byte1, byte2 };
bytes.SetValue(str_r, i);
}
return bytes;
}
#endregion
}
一般的:
//一般的CODE为;
spellSearchBoxEx1.SpellSearchSource = new string[] { "中国", "中国fgdfs", "中阿收费的肌肤", "男生", "女生" };
3:数据库情况下,本实例用的是随机生成中文模拟数据库情况;
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
//一般的CODE为;
spellSearchBoxEx1.SpellSearchSource = new string[] { "中国", "中国fgdfs", "中阿收费的肌肤", "男生", "女生" };
// 连接数据库情况下仅此;
spellSearchBoxExBySql.SpellSearchSource = GetSpellBoxSource(GetDataTable());
}
public string[] GetSpellBoxSource(DataTable dt)
{
List<string> list = new List<string>();
foreach (DataRow dr in dt.Rows)
{
if (!Convert.IsDBNull(dr["Text"]))
list.Add(dr["Text"].ToString());
}
return list.ToArray();
}
public DataTable GetDataTable()
{
//这里是自己的代码连接数据库仅得到要填的列;
//本方法没连数据库,用随机生成中文模拟获得DatatTable
// DataTable dt = DB.GetDatatable("sql");
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("Text", typeof(string)));
Random rn = new Random();
for (int i = 0; i < 50; i++)
{
string str ="中" +GetRandomChinese(rn.Next(8));
DataRow dr = dt.NewRow();
dr["Text"] = str;
dt.Rows.Add(dr);
}
return dt;
}
#region 以下只是随机生成中文,与本控件使用无关;
public string GetRandomChinese(int strlength)
{
Encoding gb = Encoding.GetEncoding("gb2312");
object[] bytes = this.CreateRegionCode(strlength);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < strlength; i++)
{
string temp = gb.GetString((byte[])Convert.ChangeType(bytes[i], typeof(byte[])));
sb.Append(temp);
}
return sb.ToString();
}
private object[] CreateRegionCode(int strlength)
{
//定义一个字符串数组储存汉字编码的组成元素
string[] rBase = new String[16] { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f" };
Random rnd = new Random();
object[] bytes = new object[strlength];
for (int i = 0; i < strlength; i++)
{
int r1 = rnd.Next(11, 14);
string str_r1 = rBase[r1].Trim();
rnd = new Random(r1 * unchecked((int)DateTime.Now.Ticks) + i);
int r2;
if (r1 == 13)
{
r2 = rnd.Next(0, 7);
}
else
{
r2 = rnd.Next(0, 16);
}
string str_r2 = rBase[r2].Trim();
rnd = new Random(r2 * unchecked((int)DateTime.Now.Ticks) + i);
int r3 = rnd.Next(10, 16);
string str_r3 = rBase[r3].Trim();
rnd = new Random(r3 * unchecked((int)DateTime.Now.Ticks) + i);
int r4;
if (r3 == 10)
{
r4 = rnd.Next(1, 16);
}
else if (r3 == 15)
{
r4 = rnd.Next(0, 15);
}
else
{
r4 = rnd.Next(0, 16);
}
string str_r4 = rBase[r4].Trim();
byte byte1 = Convert.ToByte(str_r1 + str_r2, 16);
byte byte2 = Convert.ToByte(str_r3 + str_r4, 16);
byte[] str_r = new byte[] { byte1, byte2 };
bytes.SetValue(str_r, i);
}
return bytes;
}
#endregion
}
作者:破 狼
出处:http://www.cnblogs.com/whitewolf/
本文版权归作者,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。该文章也同时发布在我的独立博客中-个人独立博客、博客园--破狼和51CTO--破狼。http://www.cnblogs.com/whitewolf/archive/2009/12/08/1619106.html