[转载]C#中,让组合框(ComboBox)支持拼音首字母检索筛选

通过继承系统的ComboBox,写一个新控件ComboBoxEx,重写它的焦点以及文本更新事件,就可以轻松实现拼音首字母检索了。例如:输入 gd ,就可以出现“广东”。

 1 using  System;
 2 using  System.Collections.Generic;
 3 using  System.Text;
 4 using  System.Windows.Forms;
 5 using  System.Collections;
 6
 7 namespace  PrintDoc
 8 ExpandedBlockStart.gifContractedBlock.gif {
 9    public class ComboBoxEx : ComboBox
10ExpandedSubBlockStart.gifContractedSubBlock.gif    {
11        private ArrayList m_list = new ArrayList();
12
13        protected override void OnEnter(EventArgs e)
14ExpandedSubBlockStart.gifContractedSubBlock.gif        {
15            m_list.Clear();
16            m_list.AddRange(this.Items);
17            base.OnEnter(e);
18        }

19
20        protected override void OnLeave(EventArgs e)
21ExpandedSubBlockStart.gifContractedSubBlock.gif        {
22            this.Items.Clear();
23            this.Items.AddRange(m_list.ToArray());
24            base.OnLeave(e);
25        }

26
27        protected override void OnTextUpdate(EventArgs e)
28ExpandedSubBlockStart.gifContractedSubBlock.gif        {
29            while (this.Items.Count > 0)
30ExpandedSubBlockStart.gifContractedSubBlock.gif            {
31                this.Items.RemoveAt(0);
32            }

33            foreach (object o in this.m_list)
34ExpandedSubBlockStart.gifContractedSubBlock.gif            {
35                if ( GetChineseSpell(o.ToString()).ToLower().Contains(this.Text.ToLower()))
36ExpandedSubBlockStart.gifContractedSubBlock.gif                {
37                    this.Items.Add(o);
38                }

39            }

40            this.DroppedDown = true;
41            this.Cursor = Cursors.Default;
42            base.OnTextUpdate(e);
43        }

44
45        static public string GetChineseSpell(string strText)
46ExpandedSubBlockStart.gifContractedSubBlock.gif        {
47            int len = strText.Length;
48            string myStr = "";
49            for (int i = 0; i < len; i++)
50ExpandedSubBlockStart.gifContractedSubBlock.gif            {
51                myStr += getSpell(strText.Substring(i, 1));
52            }

53            return myStr;
54        }

55
56        static public string getSpell(string cnChar)
57ExpandedSubBlockStart.gifContractedSubBlock.gif        {
58            byte[] arrCN = Encoding.Default.GetBytes(cnChar);
59            if (arrCN.Length > 1)
60ExpandedSubBlockStart.gifContractedSubBlock.gif            {
61                int area = (short)arrCN[0];
62                int pos = (short)arrCN[1];
63                int code = (area << 8+ pos;
64ExpandedSubBlockStart.gifContractedSubBlock.gif                int[] areacode = 4521745253457614631846826470104729747614481194811949062493244989650371506145062250906513875144652218526985269852698529805368954481 };
65                for (int i = 0; i < 26; i++)
66ExpandedSubBlockStart.gifContractedSubBlock.gif                {
67                    int max = 55290;
68                    if (i != 25) max = areacode[i + 1];
69                    if (areacode[i] <= code && code < max)
70ExpandedSubBlockStart.gifContractedSubBlock.gif                    {
71ExpandedSubBlockStart.gifContractedSubBlock.gif                        return Encoding.Default.GetString(new byte[] { (byte)(65 + i) });
72                    }

73                }

74                return "*";
75            }

76            else 
77                return cnChar;
78        }

79    }

80}

81

 

用来进行语法转化的工具:http://codeconverter.sharpdevelop.net/SnippetConverter.aspx

转化之后的VB.NET版本如下。 

 1 ExpandedBlockStart.gif ContractedBlock.gif      Public   Shared   Function getHzPy() Function getHzPy(ByVal cnChar As StringAs String
 2        Dim arrCN As Byte() = System.Text.Encoding.[Default].GetBytes(cnChar)
 3        If arrCN.Length > 1 Then
 4            Dim area As Integer = CShort(arrCN(0))
 5            Dim pos As Integer = CShort(arrCN(1))
 6            Dim code As Integer = (area << 8+ pos
 7            Dim areacode As Integer() = {452174525345761463184682647010, _
 8             472974761448119481194906249324, _
 9             498965037150614506225090651387, _
10             514465221852698526985269852980, _
11             5368954481}
12            For i As Integer = 0 To 25
13                Dim max As Integer = 55290
14                If i <> 25 Then
15                    max = areacode(i + 1)
16                End If
17                If areacode(i) <= code AndAlso code < max Then
18                    Return System.Text.Encoding.[Default].GetString(New Byte() {CByte((65 + i))})
19                End If
20            Next
21            Return "*"
22        Else
23            Return cnChar
24        End If
25    End Function

 

另外还有一篇相关的文章,除了计算拼音的算法不对之外,其他方面的事件考虑的比较周全。可以用上面的算法替换掉下面文章中的getHzPy方法。

http://blog.csdn.net/Flora_qxy/archive/2007/08/30/1764964.aspx

除此之外,再贴两篇和AucoCompleteComboBox相关的文章,看似很好很强大。

http://www.codeproject.com/KB/architecture/MultiColumnFlatCombo.aspx

http://www.codeproject.com/KB/combobox/akautocomplete.aspx

转载于:https://www.cnblogs.com/hejycpu/archive/2009/03/27/1423192.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值