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

简介:

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

}
复制代码

 


本文转自破狼博客园博客,原文链接:http://www.cnblogs.com/whitewolf/archive/2009/12/08/1619106.html,如需转载请自行联系原作者

目录
相关文章
|
4月前
|
C++
[Qt5&控件] Label控件显示文本内容(字符串和整数)
[Qt5&控件] Label控件显示文本内容(字符串和整数)
67 0
[Qt5&控件] Label控件显示文本内容(字符串和整数)
|
C#
正则表达式——WPF输入控件TextBox 限定输入特定字符
原文:正则表达式——WPF输入控件TextBox 限定输入特定字符 概念: 正则表达式是对字符串操作的一种逻辑公式, 就是用事先定义好的一些特定字符、及这些特定字符的组合,组成一个“规则字符串”, 这个“规则字符串”用来表达对字符串的一种过滤逻辑。
2080 0
|
C#
【msdn wpf forum翻译】TextBox中文本 中对齐 的方法
原文:【msdn wpf forum翻译】TextBox中文本 中对齐 的方法原文链接:http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/49864e35-1dbf-4292-a361-93f1a8400558 问题:TextBox中文本中对齐,使用 TextBox.HorizontalContentAlignment="Center"行不通(TextBox.VerticalContentAlignment="Center"则会起到预期的作用。
1197 0