天天看点

支持拼音检索的TextBox扩展控件-使用

我的上一个支持拼音检索的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:设计时:

支持拼音检索的TextBox扩展控件-使用
支持拼音检索的TextBox扩展控件-使用
支持拼音检索的TextBox扩展控件-使用

代码

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为;

            // 连接数据库情况下仅此;

            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];

                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);

                    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

}

支持拼音检索的TextBox扩展控件-使用

<a href="http://files.cnblogs.com/whitewolf/sample.rar" target="_blank">实例代码和dll文件下载</a>

支持拼音检索的TextBox扩展控件-使用