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

本文介绍了一个支持拼音检索的TextBox扩展控件,包括其使用方法和示例程序。该控件提供三种属性设置:MaxItemCount、SearchMode及SpellSearchSource,并演示了在不同场景下的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

我的上一个支持拼音检索的TextBox扩展控件,由于有些网友留言和发邮件问如何用,

如:菜鸟AAA等当时想到肯定大家都会用,就没上传示例程序。

原文为:http://www.cnblogs.com/whitewolf/archive/2009/12/03/1615975.html#1717373

既然有人问,那就上传下是下程序。肯定很多人都知道如何用,请越过就是,不要发弁言。

主要只有三个属性暴露出来:

1MaxItemCout:这是在多个下拉条时,显示多少条可见;

2SearchMode:检索方式:只提供了SearchMode.Contains SearchMode.StartWith包含和以其开始区别;

3:SpellSearchSource:检索的数据源,仅显现的中文等字符;

主要在它上:有几种方式设计:

1:设计时:

 

 

ExpandedBlockStart.gif 代码
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

}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值