c#中TextBox输入提示功能

 设置TextBox的AutoCompleteSource的属性为CustomSource,设置TextBox的AutoCompleteMode属性为SuggestAppend。

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. using WindowsApplication7.DataSet1TableAdapters;
  9. namespace WindowsApplication7
  10. {
  11.     public partial class Form1 : Form
  12.     {
  13.         DataSet1 ds = new DataSet1();
  14.         CustomersTableAdapter adpter = new CustomersTableAdapter();
  15.         public Form1()
  16.         {
  17.             InitializeComponent();
  18.           
  19.         }
  20.         private void BuildAutoCompleteList()
  21.         {
  22.             AutoCompleteStringCollection filterVals = new AutoCompleteStringCollection();
  23.             foreach (DataRow dr in ds.Customers)
  24.             {
  25.                 filterVals.Add(dr["CompanyName"].ToString());
  26.             }
  27.             txtCompanyName.AutoCompleteCustomSource = filterVals;
  28.         }
  29.         private void Form1_Load(object sender, EventArgs e)
  30.         {
  31.             adpter.Fill(ds.Customers);
  32.             BuildAutoCompleteList();
  33.          }
  34.     }
  35. }

你可能感兴趣的:(c#中TextBox输入提示功能)