有時候在項目裡面需要用到類似於百度那種自動索引的功能,在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()); } } }}
例子源碼