程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> .NET實例教程 >> 自動完成的combox,同時自動下拉

自動完成的combox,同時自動下拉

編輯:.NET實例教程
using System;
using System.Collections.Generic;
using System.Text;


namespace Costaco.CTN.Controls
{
    /// <summary>
    /// ComboBox 的摘要說明。
    /// </summary>
    public class ComboBox : System.Windows.Forms.ComboBox
    {

        #region 字段
        /// <summary>
        /// 表示最近一次的用戶交互操作是否是鍵盤操作。
        /// </summary>
        private bool isKeyPressed;
        /// <summary>
        /// 表示是否啟用自動完成功能。
        /// </summary>
        private bool autoComplete = true;
        #endregion

        #region 屬性
        /// <summary>
        /// 獲取是否啟用自動完成功能。
        /// </summary>
        //[Bindable(true), Browsable(true), SRCategory(SR.CatBehavior), SRDescription(SR.ComboBoxAutoCompleteDescr)]
        public bool AutoComplete
        {
            get
            {
                return autoComplete;
            }
            set
            {
                autoComplete = value;
            }

$False$


        }
        #endregion

        #region 構造函數
        /// <summary>
        ///
        /// </summary>
        public ComboBox()
        {
            isKeyPressed = false;

            autoComplete = true;
        }
        #endregion

        #region 保護方法
        /// <summary>
        ///
        /// </summary>
        /// <param name="e"></param>
        protected override void OnTextChanged(EventArgs e)
        {
            bool flag = isKeyPressed;

            isKeyPressed = false;

            if (DropDownStyle != System.Windows.Forms.ComboBoxStyle.DropDown)
            {
                base.OnTextChanged(e);
                return;
            }
            if (!flag)
            {
                base.OnTextChanged(e);
     ;         return;
            }
            if (!autoComplete)
            {
                base.OnTextChanged(e);
                return;
            }

            BeginUpdate();

            string newText = Text;

            if (newText != "")
            {

                if (FindStringExact(newText) != -1)
                {
                    if (DroppedDown)
                    {
                        DroppedDown = false;
                    }
                }
                else if (FindString(newText) != -1)
                {

                    DroppedDown = true;

               &nbsp;    Text = newText;

                    Select(newText.Length, 0);

                }
            }
            else if (DroppedDown)
            {
                DroppedDown = false;
            }

            EndUpdate();

            base.OnTextChanged(e);

        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="e"></param>
        protected override void OnKeyPress(System.Windows.Forms.KeyPressEventArgs e)
        {
            isKeyPressed = true;
            DroppedDown = true;

            base.OnKeyPress(e);
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="e"></param>
        protected override void OnMouseDown(System.Windows.Forms.MouseEventArgs e)
        {
            isKeyPressed = false;/>            DroppedDown = true;

            base.OnMouseDown(e);
        }
        #endregion

    }

}

 

//[Bindable(true), Browsable(true), SRCategory(SR.CatBehavior), SRDescription(SR.ComboBoxAutoCompleteDescr)]
找不到引用的,哪位大蝦如果能幫忙的話,請給我留言

修改了一點點網絡上的代碼,默認自動完成

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved