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

 1using System;
 2using System.Collections.Generic;
 3using System.Text;
 4using System.Windows.Forms;
 5using System.Collections;
 6
 7namespace PrintDoc
 8{
 9    public class ComboBoxEx : ComboBox
10    {
11        private ArrayList m_list = new ArrayList();
12
13        protected override void OnEnter(EventArgs e)
14        {
15            m_list.Clear();
16            m_list.AddRange(this.Items);
17            base.OnEnter(e);
18        }
19
20        protected override void OnLeave(EventArgs e)
21        {
22            this.Items.Clear();
23            this.Items.AddRange(m_list.ToArray());
24            base.OnLeave(e);
25        }
26
27        protected override void OnTextUpdate(EventArgs e)
28        {
29            while (this.Items.Count > 0)
30            {
31                this.Items.RemoveAt(0);
32            }
33            foreach (object o in this.m_list)
34            {
35                if ( GetChineseSpell(o.ToString()).ToLower().Contains(this.Text.ToLower()))
36                {
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)
46        {
47            int len = strText.Length;
48            string myStr = "";
49            for (int i = 0; i < len; i++)
50            {
51                myStr += getSpell(strText.Substring(i, 1));
52            }
53            return myStr;
54        }
55
56        static public string getSpell(string cnChar)
57        {
58            byte[] arrCN = Encoding.Default.GetBytes(cnChar);
59            if (arrCN.Length > 1)
60            {
61                int area = (short)arrCN[0];
62                int pos = (short)arrCN[1];
63                int code = (area << 8) + pos;
64                int[] areacode = { 45217, 45253, 45761, 46318, 46826, 47010, 47297, 47614, 48119, 48119, 49062, 49324, 49896, 50371, 50614, 50622, 50906, 51387, 51446, 52218, 52698, 52698, 52698, 52980, 53689, 54481 };
65                for (int i = 0; i < 26; i++)
66                {
67                    int max = 55290;
68                    if (i != 25) max = areacode[i + 1];
69                    if (areacode[i] <= code && code < max)
70                    {
71                        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    Public Shared Function getHzPy()Function getHzPy(ByVal cnChar As String) As 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() = {45217, 45253, 45761, 46318, 46826, 47010, _
 8             47297, 47614, 48119, 48119, 49062, 49324, _
 9             49896, 50371, 50614, 50622, 50906, 51387, _
10             51446, 52218, 52698, 52698, 52698, 52980, _
11             53689, 54481}
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

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

  1. Java中获取GBK编码汉字的拼音首字母(包括生僻字)

    Java中获取GBK编码汉字的拼音首字母(包括生僻字) 前言 代码 结果 前言 网上关于Java中获取汉字的拼音首字母的方法很多,但大多基于GB2312的汉字所属编码位置判断方法,现有一种基于GBK编 ...

  2. php 汉字按字母排序,在PHP中,将一个汉字数组按照拼音首字母进行排序

    (之前发的这篇博文因为含有敏感关键字,只好重发一遍了) $str = "我们可以在浏览器中看到,当鼠标移到元素上时,元素开始向右移动,开始比较慢,之后则比较快,移开时按原曲线回到原点.&qu ...

  3. input框输入中文内容,另一个input框中时时显示转换后的拼音首字母缩写

    最近项目中需要完成在input标签内输入中文字符,动态的在另一个input中时时显示中文字符的首字母缩写(首字母都是大写).直接上代码: value属性可以写成value="",图 ...

  4. html下拉控件 拼音检索和中文检索,Combobox控件实现汉字按拼音首字母检索

    Combobox控件在开发中作为下拉选项的不二之选,用的非常频繁,前几日开发过程中刚好有个需求有用到这个控件,而且客户要求增加下拉选择功能,这个简单,设置控件的自动完成属性后就解决了this.comb ...

  5. excel中如何实现提取汉字的拼音首字母

    http://zhidao.baidu.com/question/104836508.html 汉字中还包含英文字母或数字,英文或数字部分也要返回. 比如:中国航天6号a 返回应为:ZGHT6Ha 下 ...

  6. wps中如何实现提取汉字的拼音首字母

    步骤: 第一步.启动wps,打开相应的工作表: 第二步.执行"工具→宏→Visual Basic编辑器"命令(或者直接按"Alt+F11"组合键),进入Visu ...

  7. VC组合框ComboBox控件用法

    1.关于CComboBox在对话框中没有下拉项目的问题 答: 资源编辑器中对话框中CComboBox组件的垂直范围拉大了下,就看见字体了.先点击右边向下的箭头,然后会出现上下可调的双向箭头,拖拉即可. ...

  8. excel 中vb组合框_Excel数据验证组合框代码

    excel 中vb组合框 Instead of selecting a product code in an Excel drop down list, it's usually easier to ...

  9. excel 中vb组合框_在Excel 2010中修复组合框大小调整

    excel 中vb组合框 With Excel data validation, you can create drop down lists on a worksheet. However, the ...

最新文章

  1. 【ASP.NET开发】ASP.NET(MVC)三层架构知识的学习总结
  2. 最大熵模型:读书笔记
  3. Face-landmarks-detection-benchmark 人脸特征定位网站汇总
  4. 如何直接soap字符串,访问webservice
  5. 【Python可视化】Windows 10系统上Pyecharts安装教程
  6. 了解如何使用Vue.js CLI
  7. ASP.Net/C# - PayPal接口文档
  8. 信息学奥数一本通(1170:计算2的N次方)
  9. 宋体、代码-iOS网络编程实践--NSStream实现TCP Socket iPhone客户端-by小雨
  10. 阶段5 3.微服务项目【学成在线】_day01 搭建环境 CMS服务端开发_22-页面查询服务端开发-Dao-基础方法测试...
  11. Java 读取Excel ( xls 和 xlsx 格式 )
  12. java面试,经常遇到面试官的问题
  13. 2017年中兴算法大赛 迪杰特斯拉派
  14. 黄金圈理论和知识体系
  15. Resharper简介
  16. Codeforces Round #766 (Div. 2) B. Not Sitting
  17. java大麦_大麦大 - SegmentFault 思否
  18. 新概念英语第一册(26)
  19. 分子量(UVa1586)
  20. ISA指令集基础应用

热门文章

  1. java百度云文件上传_关于如何在自己项目集成百度云BCE文件上传STS方案
  2. 一个程序员的爱情表白书
  3. c++ cdi+示例_C ++'not'关键字和示例
  4. 打开eclipse出现Failed to load the JNI shared library “D:\java\jdk\bin\...\jre\bin\server\jvm.dll”如何解决?
  5. Java——线程的四种不同形式
  6. ajax为什么有时候不行,为什么不能用ajax调用
  7. 1.1.1.1校园网_Apache Flink 1.11.0 重要功能全面解析
  8. golang调用matlab,Golang中Proto编写和生成
  9. UVA - 210:Concurrency Simulator
  10. C++ JSON库:JSON for Morden C++