(源碼)WinForm TextBox 實現自動索引功能

來源:互聯網
上載者:User

有時候在項目裡面需要用到類似於百度那種自動索引的功能,在WinForm裡面我採用的是用一個TextBox和一個ListBox結合來實現的,大致效果如所示:

 

詳細的代碼如下:

using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;namespace AutoComplete{    public partial class Form1 : Form    {        public Form1()        {            InitializeComponent();        }        DataTable dt = new DataTable();        private void Form1_Load(object sender, EventArgs e)        {            BindDataList();        }        private void NewMethod(string CodeName)        {            DataTable dt_New = new DataTable();            dt_New = dt.Clone();            try            {                //複製一張Table對其進行篩選,條件則是使用者輸入的字串,模糊查詢。                DataRow[] dr = dt.Select("TCode like '%" + CodeName + "%'");                for (int i = 0; i < dr.Length; i++)                {                    dt_New.ImportRow((DataRow)dr[i]);                }            }            catch (Exception ex)            {                MessageBox.Show("Error!  " + ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);                return;            }            if (dt_New.Rows.Count > 0)            {                //如果資料為空白則不顯示ListBox                this.lsbCode1.Visible = true;                this.lsbCode1.Items.Clear();                for (int i = 0; i < dt_New.Rows.Count; i++)                {                    //有時候字元長度不一致導致布局很難看,這裡統一增加一些空格,看上去整齊一點。                    this.lsbCode1.Items.Add(dt_New.Rows[i][0].ToString().Trim().PadRight(10) + dt_New.Rows[i][1].ToString());                }            }            else            {                this.lsbCode1.Visible = false;            }        }        /// <summary>        /// 測試資料,實際應用中可以指定資料庫裡面的資料。        /// </summary>        void BindDataList()        {            dt.Columns.Add("TCode");            dt.Columns.Add("Description");            dt.Rows.Add("AL01", "SAP Alert Monitor SAP警示監視器");            dt.Rows.Add("AL02", "Database alert monitor 資料庫警報監測器");            dt.Rows.Add("AL03", "Operating system alert monitor 作業系統警告監視器");            dt.Rows.Add("AL04", "Monitor call distribution 監視呼叫分配");            dt.Rows.Add("AL05", "Monitor current workload 監視當前的工作負載");            dt.Rows.Add("CO03", "顯示生產訂單");            dt.Rows.Add("C005N", "Collective Release");            dt.Rows.Add("CO11N", "Time Ticket");            dt.Rows.Add("CO12", "Confirmation - Collective");            dt.Rows.Add("CO13", "Confirmation - Cancel");            dt.Rows.Add("COOIS", "Production order information system");            dt.Rows.Add("SE01", "Transport Organizer 傳送召集人");            dt.Rows.Add("SE03", "Workbench Organizer: Tools 工作台組織器:工具");            dt.Rows.Add("SE06", "Set Up Workbench Organizer 設定工作台組織器");            dt.Rows.Add("SE07", "Transport System Status Display 傳輸系統狀態顯示");            dt.Rows.Add("SE09", "Workbench Organizer 工作平台召集人");            dt.Rows.Add("SE10", "Customizing Organizer 自訂群組織者");            dt.Rows.Add("SE11", "資料庫瀏覽");            dt.Rows.Add("SE16", "Data Browser: Initial Screen");            dt.Rows.Add("SE16N", "Table Browser (the N stands for New, it replaces SE16)");            dt.Rows.Add("SE17", "General Table Display 通用表顯示");            dt.Rows.Add("MM01", "Create Material 建立物料資訊");            dt.Rows.Add("MM02", "Modify Material 修改物料資訊");            dt.Rows.Add("MM03", "Display Material 顯示物料資訊");            dt.Rows.Add("ME11", "建立採購資訊記錄");            dt.Rows.Add("ME01", "維護貨源清單");            dt.Rows.Add("ME51N", "建立採購申請");            dt.Rows.Add("ME5A", "顯示採購申請清單");        }        private void txtCode_TextChanged(object sender, EventArgs e)        {            this.lsbCode1.Visible = false;            if (txtCode.Text == "")            {                //如果條件為空白則清空已有資料,並隱藏ListBox                this.lsbCode1.Items.Clear();                this.lsbCode1.Visible = false;            }            else            {                //條件不為空白則執行篩選資料的函數。                NewMethod(this.txtCode.Text.Trim());            }        }    }}

例子源碼

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.