ComboBox控件扩展(支持拼音简写筛选)

UComboBoxEx是一个扩展了ComboBox控件的类,它支持通过输入拼音简写来筛选列表项。控件内部使用ListBox显示匹配的列表,并在用户选择或按下Enter键时更新文本。GetFillList方法根据输入的字符串获取匹配的列表项,同时考虑了全拼和首字母简写。此外,控件还处理了位置改变、焦点变化和键盘事件等交互逻辑。

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

public partial class UComboBoxEx : ComboBox
    {
        private System.Windows.Forms.ListBox lbMain;
        public UComboBoxEx()
        {
            InitializeComponent();
            lbMain = new ListBox();
            lbMain.Click += new EventHandler(lbMain_Click);
            lbMain.KeyDown += new KeyEventHandler(lbMain_KeyDown);
            lbMain.Visible = false;
        }

        public UComboBoxEx(IContainer container)
        {
            container.Add(this);
            InitializeComponent();
            lbMain = new ListBox();
            lbMain.Click += new EventHandler(lbMain_Click);
            lbMain.KeyDown += new KeyEventHandler(lbMain_KeyDown);
            lbMain.Visible = false;
        }

        /// <summary>
        /// 得到匹配给定字符串的列表
        /// </summary>
        private ArrayList GetFillList(string strValue)
        {
            ArrayList alReturn = new ArrayList();
            int iCount = this.Items.Count;
            int iLen = strValue.Length;
            string strSelItem ="";
            for (int i = 0; i < iCount; i++)
            {
                if (this.Items[i] is DataRowView)
                {
                    strSelItem = (this.Items[i] as  DataRowView)[this.DisplayMember].ToString();
                }
                else
                {
                    strSelItem = this.Items[i].ToString();
                }
                if (strSelItem.Length < iLen)
                    continue;
                if (strSelItem.IndexOf(strValue, 0, StringComparison.OrdinalIgnoreCase) >= 0 || GetChineseSpell(strSelItem).IndexOf(strValue, 0, StringComparison.OrdinalIgnoreCase) >= 0)
                    alReturn.Add(strSelItem);
            }
            return alReturn;
        }


        private void lbMain_Click(object sender, System.EventArgs e)
        {
            if (lbMain.SelectedItems.Count == 0)
                return;
            string strSel = lbMain.SelectedItem.ToString();
            this.Text = strSel;
            int iSel = this.FindStringExact(Text);
            if (iSel != -1)
                this.SelectedIndex = iSel;
            lbMain.Visible = false;
        }


        protected override void OnTextChanged(EventArgs e)
        {
            if (!DesignMode)
            {
                if (Text == "")
                {
                    lbMain.Visible = false;
                    return;
                }
                if (!this.Parent.Controls.Contains(lbMain))
                {
                    lbMain.Width = this.Width;
                    lbMain.Height = 100;
                    lbMain.Parent = this.Parent;
                    lbMain.Top = this.Top + this.Height + 1;
                    lbMain.Left = this.Left;
                    this.Parent.Controls.Add(lbMain);
                    lbMain.BringToFront();
                }
                ArrayList alFill = GetFillList(Text);
                lbMain.Items.Clear();
                lbMain.Items.AddRange(alFill.ToArray());
                if (lbMain.Items.Count > 0)
                    lbMain.SelectedIndex = 0;
                if (!lbMain.Visible && this.Focused)
                    lbMain.Visible = true;
            }
            base.OnTextChanged(e);
        }

        protected override void OnLocationChanged(EventArgs e)
        {
            if (!DesignMode)
            {
                if (lbMain != null)
                {
                    SetlbMainLocation();
                }
            }
        }

        private void SetlbMainLocation()
        {
            lbMain.Visible = false;
            lbMain.Width = this.Width;
            lbMain.Height = 100;
            lbMain.Top = this.Top + this.Height + 1;
            lbMain.Left = this.Left;
            lbMain.BringToFront();
        }

        protected override void OnLeave(EventArgs e)
        {
            if (!DesignMode)
            {
                if (!lbMain.Focused && !this.Focused)
                    lbMain.Visible = false;
            }
            base.OnLeave(e);
        }
        protected override void OnDropDown(EventArgs e)
        {
            lbMain.Visible = false;
            base.OnDropDown(e);
        }

        protected override void OnVisibleChanged(EventArgs e)
        {
            if (!DesignMode)
            {
                if (!this.Visible)
                {
                    if (lbMain != null)
                    {
                        lbMain.Visible = false;
                    }
                }
            }
            base.OnVisibleChanged(e);
        }

        protected override void OnKeyDown(KeyEventArgs e)
        {
            if (lbMain.Visible)
            {
                if (e.KeyCode == Keys.Up || e.KeyCode == Keys.Down || e.KeyCode == Keys.Left ||
                     e.KeyCode == Keys.Right || e.KeyCode == Keys.PageDown || e.KeyCode == Keys.PageUp)
                {
                    lbMain_KeyDown(lbMain, e);
                    e.Handled = true;
                }
                else if (e.KeyCode == Keys.Enter)
                {
                    lbMain_Click(lbMain, e);
                    e.Handled = true;
                }
            }
            base.OnKeyDown(e);
        }

        private void lbMain_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Up || e.KeyCode == Keys.Left || e.KeyCode == Keys.PageUp)
            {
                if (lbMain.SelectedIndex > 0)
                    lbMain.SelectedIndex = lbMain.SelectedIndex - 1;
            }
            else if (e.KeyCode == Keys.Down || e.KeyCode == Keys.Right || e.KeyCode == Keys.PageDown)
            {
                if (lbMain.SelectedIndex < lbMain.Items.Count - 1)
                    lbMain.SelectedIndex = lbMain.SelectedIndex + 1;
            }
        }

        private void UComboBoxEx_SelectedIndexChanged(object sender, System.EventArgs e)
        {
            if (!DesignMode)
            {
                lbMain.Visible = false;
            }
        }

        static public string GetChineseSpell(string strText)
        {
            int len = strText.Length;
            string myStr = "";
            for (int i = 0; i < len; i++)
            {
                myStr += getSpell(strText.Substring(i, 1));
            }
            return myStr;
        }

        static public string getSpell(string cnChar)
        {
            byte[] arrCN = Encoding.Default.GetBytes(cnChar);
            if (arrCN.Length > 1)
            {
                int area = (short)arrCN[0];
                int pos = (short)arrCN[1];
                int code = (area << 8) + pos;
                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 };
                for (int i = 0; i < 26; i++)
                {
                    int max = 55290;
                    if (i != 25) max = areacode[i + 1];
                    if (areacode[i] <= code && code < max)
                    {
                        return Encoding.Default.GetString(new byte[] { (byte)(65 + i) });
                    }
                }
                return "*";
            }
            else
                return cnChar;
        }
    }

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值